Python program to convert a list of characters into a string

Here, we are going to learn how to convert the characters list (a list of characters) into a string in Python programming language? By IncludeHelp Last updated : February 25, 2024

Python | Converting the characters list into a string

To convert a given list of characters into a string, there are two approaches,

  1. Using the loop – traverse of the list i.e. extracts characters from the list and add characters to the string.
  2. Using join() function – a list of characters can be converted into a string by joining the characters of the list in the string.

Note: In both the above cases, the string should be declared, (you can assign "" to declare it as an empty string).

Using Loop

list1 = ['H', 'e', 'l', 'l', 'o']

# printing characters list and its type
print("list1: ", list1)
print("type(list1): ", type(list1))
print()

# converting character list to the string
str1 = ""
for i in list1:
  str1 += i;

# print the string and its type
print("str1: ", str1)
print("type(str1): ", type(str1))

Output

list1:  ['H', 'e', 'l', 'l', 'o']
type(list1):  <class 'list'>

str1:  Hello
type(str1):  <class 'str'>

Using join() Method

list1 = ['H', 'e', 'l', 'l', 'o']

# printing characters list and its type
print("list1: ", list1)
print("type(list1): ", type(list1))
print()

# converting character list to the string
str1 = ""
str1 = str1.join(list1)

# print the string and its type
print("str1: ", str1)
print("type(str1): ", type(str1))

Output

list1:  ['H', 'e', 'l', 'l', 'o']
type(list1):  <class 'list'>

str1:  Hello
type(str1):  <class 'str'>

To understand the above programs, you should have the basic knowledge of the following Python topics:

Python String Programs »

Related Programs

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.