Python | Convert character list to string

Python Methods and Functions | String Variables

Examples:

 Input: ['g',' e', 'e',' k', 's',' f', 'o',' r', 'g' , 'e',' e', 'k',' s'] Output: pythonengineering Input: ['p',' r', 'o',' g', 'r',' a', 'm' , 'm',' i', 'n',' g'] Output: programming 

Method 1: traversing the list

Initialize an empty string at the beginning. Loop through the list of characters, for each index add a character to the original string. When the traversal is complete, print the line that was added with each character.

# Python list conversion program
number of characters

  

def convert (s):

 

# initialize the string to & quot; & quot;

  new = ""

 

  # line move

for x in s:

new + =

 

# return string

return new

 

 
# driver code

s = [ 'g' , ' e' , 'e' , ' k' , 's' , ' f' , 'o' , 'r' , 'g' , ' e' , 'e' , ' k' , 's' ]

print (convert (s))

Exit :

 pythonengineering 

Method 2. Using the join () function

Using the function isalnum-isspace-join/> join () in python, it is possible to concatenate all characters in a list. Syntax:

  str = "" str1 = ("geeks", "for", "geeks") str.join (str1) 

List of symbols can be easily concatenated by initializing str = ”” so that there are no spaces between them.

# Python list conversion program
number of characters

 

def convert (s):

 

# initialize string to & quot; & quot;

str1 = ""

 

# using the function connect join the list

# splitting words by str1

return (str1.join (s))

 
# driver code

s = [ ' g' , 'e' , ' e' , 'k' , ' s' , 'f' , 'o' , ' r' , ' g' , 'e' , ' e' , 'k' , ' s' ]

print (convert (s))

Exit:

 pythonengineering 




Tutorials