How do I split a string into a list of characters?
In Python, you can use the built-in list()
function to convert a string into a list of characters. Here is an example:
This will output the following:
['h', 'e', 'l', 'l', 'o']
Watch a video course
Python - The Practical Guide
Alternatively, you can also use a for loop to iterate over the string and append each character to a new list.
string = "hello"
char_list = []
for char in string:
char_list.append(char)
print(char_list)
or use list comprehension
Both the above methods will give the same output as the first method.