Python | Splitting string to list of characters
Sometimes we get a string and we need to split it into the individual processing. This is quite a common utility and has application in many domains, be it Machine Learning or Web Development. Having shorthands to it can be helpful. Let’s discuss certain ways in which this can be done.
Method #1 : Using list()
This is the simplest way to achieve this particular task using the internal implementation of the inbuilt list function which helps in breaking a string into its character components.
# Python3 code to demonstrate # split string to character list # using list() # initializing string test_string = 'GeeksforGeeks' # printing the original string print ("The original string is : " + str(test_string)) # using list() # to split string to character list res = list(test_string) # printing result print ("The splitted character's list is : " + str(res)) |
The original string is : GeeksforGeeks
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 perform this particular task. The map function needs to be feeded with None value to perform this task as first argument and the target string as the last argument. Works for Python2 only.
# Python code to demonstrate # split string to character list # using map() # initializing string test_string = 'GeeksforGeeks' # printing the original string print ("The original string is : " + str(test_string)) # using map() # to split string to character list res = list(map(None, test_string)) # printing result print ("The splitted character's list is : " + str(res)) |
The original string is : GeeksforGeeks
The splitted character’s list is : [‘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 | Convert a list of characters into a string
- Python | Split string into list of characters
- Python | Splitting operators in String
- Python | Splitting Text and Number in string
- Python | Convert list of strings and characters to list of characters
- List comprehension and ord() in Python to remove all characters other than alphabets
- Python | Extract only characters from given string
- Iterate over characters of a string in Python
- Python | Convert list of string to list of list
- Python | Convert list of string into sorted list of integer
- Python | Convert list of numerical string to list of Integers
- Python | Return lowercase characters from given string
- Check if both halves of the string have same set of characters in Python
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.



