Strings are used in python to handle text data. In this article, we will discuss different ways to split a string into characters in Python.
Can We Split a String Into Characters Using the split() Method?
In python, we usually use the split()
method on a string to split it into substrings. The split()
method, when invoked on a string, takes a character as a separator as its input argument. After execution, it splits the string at all the instances where the separator is present and returns a list of substrings. For instance, consider the following example.
myStr="Python For Beginners"
print("The input string is:",myStr)
output=myStr.split()
print("The output is:",output)
Output:
The input string is: Python For Beginners
The output is: ['Python', 'For', 'Beginners']
One can say that we can use an empty string to split a string into characters. Let us try that.
myStr="Python For Beginners"
print("The input string is:",myStr)
output=myStr.split("")
print("The output is:",output)
Output:
ValueError: empty separator
In this example, we have passed an empty string as a separator to split the string into characters. However, the program runs into a ValueError exception saying that you have used an empty separator.
So, we cannot use the split()
method to split a python string into characters. Let us discuss other approaches for this.
Split a String Into Characters Using the for Loop in Python
A string in python is an iterable object. Hence, we can access the character of a string one by one using a for loop.
To split a string using the for loop, we will first define an empty list to contain the output characters. Then, we will iterate through the characters of the string using a python for loop. While iteration, we will add each character of the string to the list using the append()
method. The append()
method, when invoked on a list, takes a character as its input argument and appends it to the end of the list.
After execution of the for loop, we will get all the characters of the string in the list. You can observe this in the following example.
output=[]
myStr="Python For Beginners"
print("The input string is:",myStr)
for character in myStr:
output.append(character)
print("The output is:",output)
Output:
The input string is: Python For Beginners
The output is: ['P', 'y', 't', 'h', 'o', 'n', ' ', 'F', 'o', 'r', ' ', 'B', 'e', 'g', 'i', 'n', 'n', 'e', 'r', 's']
In the above example, we have split the string "Python For Beginners"
into a list of characters.
String to List of Characters Using List Comprehension
List comprehension is used to create a list from elements of an existing iterable object in python. It’s a better alternative to the approach using a for loop and the append()
method as we can convert any iterable object into a list with a single python statement.
You can split a string into a list of characters using list comprehension as shown below.
myStr="Python For Beginners"
print("The input string is:",myStr)
output=[character for character in myStr]
print("The output is:",output)
Output:
The input string is: Python For Beginners
The output is: ['P', 'y', 't', 'h', 'o', 'n', ' ', 'F', 'o', 'r', ' ', 'B', 'e', 'g', 'i', 'n', 'n', 'e', 'r', 's']
In this example, we have used list comprehension instead of the for loop. Hence, we are able to get the same results in fewer lines of code.
String to List of Characters Using the list() Function in Python
The list()
constructor is used to create a list from any iterable object such as a string, set, or tuple in python. It takes the iterable object as its input argument and returns a list containing the elements of the iterable object.
To break a string into characters, we will pass the input string to the list()
function. After execution, it will return a list containing all the characters of the input string. You can observe this in the following example.
myStr="Python For Beginners"
print("The input string is:",myStr)
output=list(myStr)
print("The output is:",output)
Output:
The input string is: Python For Beginners
The output is: ['P', 'y', 't', 'h', 'o', 'n', ' ', 'F', 'o', 'r', ' ', 'B', 'e', 'g', 'i', 'n', 'n', 'e', 'r', 's']
Using the tuple() Function
A tuple is an immutable version of a list. Hence, you can also convert a string into a character of tuples. For this, you can use the tuple()
function. The tuple()
function takes the iterable object as its input argument and returns a tuple containing the elements of the iterable object.
To split a string, we will pass the input string to the tuple()
function. After execution, it will return a tuple containing all the characters of the input string. You can observe this in the following example.
myStr="Python For Beginners"
print("The input string is:",myStr)
output=tuple(myStr)
print("The output is:",output)
Output:
The input string is: Python For Beginners
The output is: ('P', 'y', 't', 'h', 'o', 'n', ' ', 'F', 'o', 'r', ' ', 'B', 'e', 'g', 'i', 'n', 'n', 'e', 'r', 's')
Conclusion
In this article, we have discussed different ways to split a string into characters in python.
To learn more about python programming, you can read this article on string manipulation. You might also like this article on python simplehttpserver.
I hope you enjoyed reading this article. Stay tuned for more informative articles.
Happy Learning!
Recommended Python Training
Course: Python 3 For Beginners
Over 15 hours of video content with guided instruction for beginners. Learn how to create real world applications and master the basics.