Split a string into list of characters in Python
If you're working with strings in Python, you may sometimes need to split them into a list of individual characters. This can be done with the built-in list() function. Simply pass the string you want to split into the list() function and it will return a list of characters.
Solution 1: Split a string into list of characters using list() function
The list() function is a built-in function in Python that takes an iterable object (like a string) and returns a list. The list() function is often used to split a string into a list of characters. For example, if you have a string "Hello" and you want to split it into a list of characters, you can use the list() function: list("Hello") which would return ['H', 'e', 'l', 'l', 'o'].
Syntax
list(my_string)
Code example
# create a string
str_a = "devsheet"
# split the string into a list of characters
result = list(str_a)
# print the result
print(result)
Output
['d', 'e', 'v', 's', 'h', 'e', 'e', 't']
This code example creates a string variable called str_a and assigns the string "devsheet" to it. It then uses the built-in list() function to convert the string into a list of individual characters. Finally, it prints the list.
Solution 2: Using List comprehension
Python is a versatile language that you can use to split a string into a list of characters. List comprehension is a powerful tool that you can use to manipulate strings. Here, you will learn how to split a string into a list of characters using list comprehension in Python.
Code example
# create a string
str_a = "devsheet"
# split the string into a list of characters
result = [x for x in str_a]
# print the result
print(result)
Output
['d', 'e', 'v', 's', 'h', 'e', 'e', 't']
The code example above is creating a list of characters from a string. The result variable is a list of characters from the string stored in the str_a variable.
Solution 3: Split string into a list of characters using For Loop
We can split a string into a list of characters using a python For loop. This is a simple process that can be performed on any string data type. First, we initialize an empty list. Then, we iterate through the string, appending each character to the list. Finally, we print the list to confirm that the characters have been correctly split.
Code example
# create a string
my_str = "devsheet"
result = []
for char in my_str:
result.append(char)
print(result)
Output
['d', 'e', 'v', 's', 'h', 'e', 'e', 't']
- This code creates a string called my_str, which is set to "devsheet". It then creates an empty list called result.
- The code then loops through each character in my_str and appends it to the result list.
- Finally, the code prints out the result list.
- Split string to list python
- String characters to list items using python
- Loop through string characters in Python
- Convert all string characters to lowercase in python
- Get first N characters from a string in Python
- Python - regex , remove all single characters,replace all single chars,char
- Create a flat list from list of lists in Python