Home » Python » Python programs

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?
Submitted by IncludeHelp, on April 13, 2020

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).

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






Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.








Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.