Python | Split a string into a list of characters

Examples :

  Input:  geeks  Output:  ['g',' e', 'e' , 'k',' s']  Input:  Word  Output:  ['W',' o', 'r',' d'] 

Code # 1: Using a For Loop

This approach uses a loop to convert each character to a list. Simply enclosing the For loop in square brackets [] will split the characters of the word into a list.

# Python3 program for splitting a string into characters

def split (word):

return [char for char in word] 

  
# Driver code

word = 'geeks'

print ( split (word))

Exit:

 ['g',' e', 'e',' k', 's'] 

Code # 2: coercion to a list
Python provides direct typing of a string into a list using list () .

# Python3 program for splitting a string into characters

def split (word):

return list (word)

 
# Driver code

word = 'geeks'

print (split (word))

Exit :

 ['g',' e', 'e',' k', 's' ] 




Get Solution for free from DataCamp guru