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’]

Python | Splitting a string into a list of characters Python functions: Questions

Python | Splitting a string into a list of characters split: Questions

Shop

Best laptop for Fortnite

$

Best laptop for Excel

$

Best laptop for Solidworks

$

Best laptop for Roblox

$

Best computer for crypto mining

$

Best laptop for Sims 4

$

Best laptop for Zoom

$499

Best laptop for Minecraft

$590

Latest questions

NUMPYNUMPY

psycopg2: insert multiple rows with one query

12 answers

NUMPYNUMPY

How to convert Nonetype to int or string?

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Javascript Error: IPython is not defined in JupyterLab

12 answers

News

Wiki

Python OpenCV | cv2.putText () method

numpy.arctan2 () in Python

Python | os.path.realpath () method

Python OpenCV | cv2.circle () method

Python OpenCV cv2.cvtColor () method

Python - Move item to the end of the list

time.perf_counter () function in Python

Check if one list is a subset of another in Python

Python os.path.join () method