How to split a string into individual characters in Python

In this tutorial, we will explore three different methods for splitting a string into individual characters in Python. We will start with a for loop, which allows us to iterate over the characters in a string and add them to a list. Then, we will look at using the list() function, which returns a list of individual characters from a string. Finally, we will explore using list comprehension, which is a concise way to create a list from an iterable, such as a string.

By the end of this tutorial, you will have a clear understanding of how to split a string into individual characters using Python.

How to split a string every character in Python

There are different ways to split a string every character in Python.

Method-1: split a string into individual characters in Python Using a for loop

Let us see an example of how to split a string into individual characters in Python using for loop.

One way to split a string into individual characters is to iterate over the string using a for loop and add each character to a list.

Here’s an example:

my_string = "United States of America"
char_list = []

for char in my_string:
    char_list.append(char)

print(char_list)

Output

['U', 'n', 'i', 't', 'e', 'd', ' ', 'S', 't', 'a', 't', 'e', 's', ' ', 'o', 'f', ' ', 'A', 'm', 'e', 'r', 'i', 'c', 'a']

In this example, we create a string called my_string with the value “United States of America”. We then create an empty list called char_list. Next, we use a for loop to iterate over each character in my_string, adding each character to char_list using the append() method. Finally, we print out char_list to verify that it contains all the individual characters from the string.

How to split a string every character in python
How to split a string every character in python

Method-2: split a string into individual characters in Python using the list() function

Now, let us see, how to split a string into individual characters in Python using the list() function.

Another way to split a string into individual characters is to use the list() function. We pass the string as an argument to list(), and it returns a list where each character is a separate element.

Here’s an example:

my_string = "hello"
char_list = list(my_string)

print(char_list)

Output:

['h', 'e', 'l', 'l', 'o']

In this example, we pass my_string as an argument to the list() function, which returns a list where each character is a separate element. We store this list in the variable char_list and print it out to verify that it contains all the individual characters from the string.

Method-3: split a string into individual characters in Python using a list comprehension

Now, let us see, how to split a string into individual characters in Python using a list comprehension.

A third way to split a string into individual characters is to use list comprehension in Python. We can create a new list by iterating over the characters in the string and adding them to the list.

Here’s an example:

my_string = "hello"
char_list = [char for char in my_string]

print(char_list)

Output:

['h', 'e', 'l', 'l', 'o']

In this example, we use a list comprehension to iterate over the characters in the string my_string and add them to a new list called char_list. We then print out char_list to verify that it contains all the individual characters from the string in Python.

Conclusion

There are several ways to split a string into individual characters in Python, including using a for loop, the list() function, and list comprehension.

You may like the following Python string tutorials: