Split String Into List of Characters In Python

The string is a list of different characters which can be numeric or the alphabet or special characters String can consist of single or more characters. The List type is a Python-specific type that is very similar to the array in other programming languages. We can use different methods in order to split a string to list of characters.

Split String Into List of Characters Using list()

The list() method is used to convert provided values or variables into the list. We can provide the string into the list() method as a parameter where the list() method returns the list where every character of the provided string is converted into the list items.

s = "pythontect"

l = list(s)

print(l)
['p', 'y', 't', 'h', 'o', 'n', 't', 'e', 'c', 't']

Split String Into List of Characters Using Comprehension

Another method to convert a string into a list of characters is comprehension. The comprehension simply iterates over all characters of the string and every iterated character is put into the list.

s = "pythontect"

l = [char for char in s]

print(l)
['p', 'y', 't', 'h', 'o', 'n', 't', 'e', 'c', 't']

Split String Into List of Characters Using for Loop

Even if not so practical the for loop can be used to split a string into a list of characters. As a string is a list of characters the characters can be iterated using the for loop.

s = "pythontect"

l = []

for c in s:
   l.append(c)

print(l)

Leave a Comment