Change language

Python | Splitting a string into a list of characters

| |

Method # 1: Using list()
This is the simplest way to solve this particular problem, using an internal implementation of the built-in list function that helps to split the string into its character components.

# Python3 demo code
# split the string into a list of characters
# using list ()

 
# initialization string

test_string = ’GeeksforGeeks’

  
# print original string

print ( "The original strin g is: " + str (test_string))

 
# using a list ()
# split the string into a list of characters

res = list (test_string)

 
# print result

print ( "The splitted character’s list is:" + str (res))

Exit:

The original string is: Python.Engineering
The splitted character’s list is: [’G’, ’e’, ​​’e’, ​​’k’, ’s’, ’f’, ’o’ , ’r’, ’G’, ’e’, ​​’e’, ​​’k’, ’s’]

Method # 2: Using map()
The map function can also be used to accomplish this specific task. The map function must be supplied with the value None to perform this task as the first argument and the target string as the last argument. Works only for Python2.

# Python code for demonstration
# split the string into a list of characters
# using map ()

 
# initialization string

test_string = ’GeeksforGeeks’

  
# print the original string

print ( "The original string is:" + str (test_string))

 
# using map ()
# split the string into a list of characters

res = list ( map ( None , test_string))

  
# print result

print ( "The splitted character’s list is:" + str (res))

Output:

The original string is: Python.Engineering
The splitted character’s list is: [’G’, ’e’, ​​’e’, ​​’ k ’,’ s’, ’f’, ’o’, ’r’, ’G’, ’e’, ​​’e’, ​​’k’, ’s’]