Appearance
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:
Convert a string into a list of characters, using built-in list() function in Python
python
string = "hello"
char_list = list(string)
print(char_list)This will output the following:
output
console
['h', 'e', 'l', 'l', 'o']Alternatively, you can also use a for loop to iterate over the string and append each character to a new list.
Using a for loop o iterate over the string and append each character to a new list in Python
python
string = "hello"
char_list = []
for char in string:
char_list.append(char)
print(char_list)or use list comprehension
Use list comprehension in Python
python
string = "hello"
char_list = [char for char in string]
print(char_list)Both the above methods will give the same output as the first method.