Splitting String to List of Characters - Python
Last Updated :
04 Feb, 2025
The task of splitting a string into a list of characters in Python involves breaking down a string into its individual components, where each character becomes an element in a list. For example, given the string s = "GeeksforGeeks", the task is to split the string, resulting in a list like this: ['G', 'e', 'e', 'k', 's', 'f', 'o', 'r', 'G', 'e', 'e', 'k', 's'].
Using list comprehension
List comprehension is a efficient way to create a new list by iterating over an iterable .When splitting a string into characters, list comprehension is particularly useful due to its clean syntax and fast performance .
Python
s = "GeeksforGeeks"
res = [char for char in s]
print(res)
Output['G', 'e', 'e', 'k', 's', 'f', 'o', 'r', 'G', 'e', 'e', 'k', 's']
Explanation: list comprehension iterate over each character in the string s, creating a new list where each character from the string is added as an individual element.
Using list()
list() constructor convert any iterable into a list. By passing a string to list(), it efficiently breaks the string into its individual characters, making it one of the simplest and most efficient ways to split a string into a list.
Python
s = "GeeksforGeeks"
res = list(s)
print(res)
Output['G', 'e', 'e', 'k', 's', 'f', 'o', 'r', 'G', 'e', 'e', 'k', 's']
Explanation: list() convert the string s into a list, where each character from the string becomes an individual element in the list.
Using map()
map() applies a given function to each item of an iterable and returns an iterator. While map() can be used for more complex transformations, it's also a good option for splitting a string into characters when combined with the str() function.
Python
s = "GeeksforGeeks"
res = list(map(str, s))
print(res)
Output['G', 'e', 'e', 'k', 's', 'f', 'o', 'r', 'G', 'e', 'e', 'k', 's']
Explanation: map()
apply the str
function to each character of the string s
, then converts the result into a list.
Using for loop
For loop is a traditional approach where we iterate over each character in the string and append it to a list. This method is more manual but can be used effectively, especially when we need to add additional logic inside the loop.
Python
s = "GeeksforGeeks"
res = []
for char in s:
res.append(char)
print(res)
Output['G', 'e', 'e', 'k', 's', 'f', 'o', 'r', 'G', 'e', 'e', 'k', 's']
Explanation: for loop iterates over each character in the string s and appends it to the list res. After the loop, the list res contains the individual characters of the string.
Similar Reads
Split String into List of characters in Python We are given a string and our task is to split this string into a list of its individual characters, this can happen when we want to analyze or manipulate each character separately. For example, if we have a string like this: 'gfg' then the output will be ['g', 'f', 'g'].Using ListThe simplest way t
2 min read
Split String of list on K character in Python In this article, we will explore various methods to split string of list on K character in Python. The simplest way to do is by using a loop and split().Using Loop and split()In this method, we'll iterate through each word in the list using for loop and split it based on given K character using spli
2 min read
Python | K Character Split String The problems and at the same time applications of list splitting is quite common while working with python strings. Some characters are usually tend to ignore in the use cases. But sometimes, we might not need to omit those characters but include them in our programming output. Letâs discuss certain
4 min read
Python | Splitting string list by strings Sometimes, while working with Python strings, we might have a problem in which we need to perform a split on a string. But we can have a more complex problem of having a front and rear string and need to perform a split on them. This can be multiple pairs for split. Let's discuss certain way to solv
3 min read
Python | Split flatten String List Sometimes, while working with Python Strings, we can have problem in which we need to perform the split of strings on a particular deliminator. In this, we might need to flatten this to a single String List. Let's discuss certain ways in which this task can be performed. Method #1 : Using list compr
7 min read
Ways to split strings on Uppercase characters - Python Splitting strings on uppercase characters means dividing a string into parts whenever an uppercase letter is encountered.For example, given a string like "CamelCaseString", we may want to split it into ["Camel", "Case", "String"]. Let's discuss different ways to achieve this.Using Regular Expression
3 min read