Python | Splitting string to list of characters
Sometimes we need to work with just the lists and hence strings might need to be converted into lists. It has to be converted in list of characters for certain tasks to be performed. This is generally required in Machine Learning to preprocess data and text classifications.
Let’s discuss certain ways in which this task is performed.
Method #1 : Using list slicing
List slicing can be used for this particular purpose, in which we assign to each index element of list the next occurring character of string using the slice operation.
# Python3 code to demonstrate # splitting string to list of characters. # using list slicing # initializing string test_string = "GeeksforGeeks" # printing original string print ("The original string is : " + str(test_string)) # using list slicing # for splitting string to list of characters res = [] res[:0] = test_string # printing result print ("The resultant list of characters : " + str(res)) |
The original string is : GeeksforGeeks
The resultant list of characters : [‘G’, ‘e’, ‘e’, ‘k’, ‘s’, ‘f’, ‘o’, ‘r’, ‘G’, ‘e’, ‘e’, ‘k’, ‘s’]
Method #2 : Using list()
The most concise and readable way to perform splitting is to type case string into list and the splitting of list is automatically handled internally. This is recommended method to perform this particular task.
# Python3 code to demonstrate # splitting string to list of characters. # using list() # initializing string test_string = "GeeksforGeeks" # printing original string print ("The original string is : " + str(test_string)) # using list() # for splitting string to list of characters res = list(test_string) # printing result print ("The resultant list of characters : " + str(res)) |
The original string is : GeeksforGeeks
The resultant list of characters : [‘G’, ‘e’, ‘e’, ‘k’, ‘s’, ‘f’, ‘o’, ‘r’, ‘G’, ‘e’, ‘e’, ‘k’, ‘s’]
Method #3 : Using map() + lambda
This is yet another way to perform this particular task. Though not recommended but can be used in certain situations. But drawback is readability of code gets sacrificed.
# Python3 code to demonstrate # splitting string to list of characters. # using map() + lambda # initializing string test_string = "GeeksforGeeks" # printing original string print ("The original string is : " + str(test_string)) # using map() + lambda # for splitting string to list of characters res = list(map(lambda i:i, test_string)) # printing result print ("The resultant list of characters : " + str(res)) |
The original string is : GeeksforGeeks
The resultant list of characters : [‘G’, ‘e’, ‘e’, ‘k’, ‘s’, ‘f’, ‘o’, ‘r’, ‘G’, ‘e’, ‘e’, ‘k’, ‘s’]
Recommended Posts:
- Python | Splitting string to list of characters
- Python | Splitting list on empty string
- Python | Splitting string list by strings
- Python - Remove front K characters from each string in String List
- Python | Convert a list of characters into a string
- Python | Split string into list of characters
- Python | Splitting operators in String
- Python - Remove Rear K characters from String List
- Python | Splitting Text and Number in string
- Python | Convert list of strings and characters to list of characters
- Python - Remove words containing list characters
- Python | Merge Range Characters in List
- Python | Convert List of String List to String List
- List comprehension and ord() in Python to remove all characters other than alphabets
- Python | Get positional characters from String
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

