How to Split a String into List of Characters in Python?
Split String into List of Characters in Python
You can split a string into a list of characters in Python in many ways.
In this tutorial, we will learn how to split a string into list of characters using for loop and List class.
Examples
1. Split given string into list of characters using For loop
In this example, we will take a string and split it into a list of characters using for loop.
Python Program
def getCharList(str):
return [char for char in str]
str = 'pythonexamples'
print(getCharList(str))
Output
['p', 'y', 't', 'h', 'o', 'n', 'e', 'x', 'a', 'm', 'p', 'l', 'e', 's']
2. Split given string into list of characters using List Class
In this example, we will take a string and pass it to the List constructor. String is considered an iterable and is converted to List with each character in the string as element of list.
Python Program
str = 'pythonexamples'
chars = list(str)
print(chars)
Output
['p', 'y', 't', 'h', 'o', 'n', 'e', 'x', 'a', 'm', 'p', 'l', 'e', 's']
Summary
In this tutorial of Python Examples, we learned how to split a string into list of characters, with the help of well detailed examples.