Q:

Python program to convert a list of characters into a string

belongs to collection: Python String Programs

0

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.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Method 1:

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'>

Method 2:

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'>

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Python String Programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Python program to check whether a variable is a st... >>
<< Python program to input a string and find total nu...