Python Split String Into List of Characters

A string value in Python is a sequence of the alphabet, number, or characters that are enclosed in single or double quotation marks. List values are ordered data collections that store more than one element of different data types in a single variable. Strings values are immutable, while Python lists are mutable. Therefore, once a string is created, it can’t be changed; however, the lists can be modified anytime.

In this write-up, you’ll learn a comprehensive guide on how a Python string can be split into a list of characters using the below-given methods:

It’s time to begin!

How to Split String into List of Characters in Python?

There are multiple ways in Python in which the string can be split into a list of characters. Some of the methods are discussed below with respective coding examples.

First, let’s look at the extend() method:

Method 1: Using extend() Function

In the example given below, the “extend()” function is used to add the specific characters of a string into an empty list.

Code:

string_value = 'itslinuxfoss'
list_value = []
list_value.extend(string_value)
print(list_value)

In the above code:

  • A string value and an empty list is initialized.
  • The “extend()” function is used to add the characters of the given string into the empty list.

Output:

As you can see, the above output shows a list of the input string’s characters.

Method 2: Using list() Function

In the example code shown below, the “list()” function is used to split a Python string into a list of characters.

Code:

word = 'PythonGuide'
print(list(word))

In the above code:

  • The string value ‘PythonGuide’ is initialized.
  • The “list()” function takes a string variable as an argument and splits all the characters into the list.

Output:

From the above output, it is clear that the given string has been split into the list of characters.

Method 3: Using For Loop

In the code given below, the “for” loop and inbuilt “append()” function are used to split a string into a list of characters.

Code:

string_value = 'Python'
list_value = []

for character in string_value:
    list_value.append(character)
print(list_value)

In the above code:

  • The string value “Python” and the empty list are initialized.
  • The “for” loop iterates on the given string and returns each character value. The “append()” function written inside the body of the for loop adds each character value of the string into an empty list.

Output:

The above output shows that the given string has been split into the list of characters.

Method 4: Using List Comprehension

In the example code below, the list comprehension method returns each string character to the list.

Code:

string_value = 'Python'
list_compreh = [i for i in string_value]
print(list_compreh)

In the above code:

  • The string value “Python” is initialized.
  • The list comprehension expression is defined inside the square bracket using “for” and “in” statements. The temporary value “i” iterates on all the characters of the string and, consequently, returns a list of characters.

Output:

The above output shows the list of characters of the given string.

That’s it for this guide!

Conclusion

To split the string into a list of characters, the “list()”, “extend()”,List Comprehension”, and “for Loop” is utilized in Python. The inbuilt “list()” function converts the Python string into a list of characters. The “extend()” function first splits the characters and then adds them into an empty list. The “List Comprehension” method creates a list of characters from the given string. This write-up explained how to split a string into a list of characters using various methods.