Strings are used for text manipulation in Python. In this article, we will discuss different ways to split a string into characters in python.
- Split String Into Characters Using for Loop
- String to List of Characters Using list() Function
- String to List of Characters Using List Comprehension
- String to List of Characters Using Unpacking Operator in Python
- String to List of Distinct Characters in Python
- String to Set of Characters in Python
- Conclusion
Split String Into Characters Using for Loop
To split a string into characters, you can use a for loop and a python list. For this, we will use the following steps.
- First, we will create an empty list named
character_list
to store the characters of the input string. - Next, we will iterate through the characters of the input string using a for loop.
- While iteration, we will append each character to
character_list
using theappend(
) method. Theappend()
method, when invoked oncharacter_list
, takes the character as its input and appends the character to the list.
After execution of the for loop, we will get the characters of the string in the list character_list
. You can observe this in the following example.
myStr="Pythonforbeginners"
print("The input string is:")
print(myStr)
character_list=[]
for character in myStr:
character_list.append(character)
print("The characters are:")
print(character_list)
Output:
The input string is:
Pythonforbeginners
The characters are:
['P', 'y', 't', 'h', 'o', 'n', 'f', 'o', 'r', 'b', 'e', 'g', 'i', 'n', 'n', 'e', 'r', 's']
In the above example, we have used the for loop and the append()
method to convert the string “Pythonforbeginners
” into a list of characters.
Instead of using the append()
method, you can also use the extend()
method without even using the for loop to split a string into characters in python. For this, you first need to create an empty string.
Then, you can invoke the extend()
method on the list and pass the string as its input argument. The extend()
method, when invoked on a list, takes an iterable object such as a string as input argument. After execution, it appends all the elements of the iterable object to the list.
You can observe this in the following example.
myStr="Pythonforbeginners"
print("The input string is:")
print(myStr)
character_list=[]
character_list.extend(myStr)
print("The characters are:")
print(character_list)
Output:
The input string is:
Pythonforbeginners
The characters are:
['P', 'y', 't', 'h', 'o', 'n', 'f', 'o', 'r', 'b', 'e', 'g', 'i', 'n', 'n', 'e', 'r', 's']
In the above example, we have used the extend()
method instead of the append()
method to obtain the list of characters from the string "Pythonforbeginners"
.
String to List of Characters Using list() Function
Instead of using a for loop and the append()
method to split a string into characters in python, you can use the list()
function. When we pass a string to the list()
function, we will get a list of characters of the input string as shown below.
myStr="Pythonforbeginners"
print("The input string is:")
print(myStr)
character_list=list(myStr)
print("The characters are:")
print(character_list)
Output:
The input string is:
Pythonforbeginners
The characters are:
['P', 'y', 't', 'h', 'o', 'n', 'f', 'o', 'r', 'b', 'e', 'g', 'i', 'n', 'n', 'e', 'r', 's']
Instead of a list, you can also split a string into characters using the tuple()
function in python as shown below.
myStr="Pythonforbeginners"
print("The input string is:")
print(myStr)
character_list=tuple(myStr)
print("The characters are:")
print(character_list)
Output:
The input string is:
Pythonforbeginners
The characters are:
('P', 'y', 't', 'h', 'o', 'n', 'f', 'o', 'r', 'b', 'e', 'g', 'i', 'n', 'n', 'e', 'r', 's')
In this example, we have obtained a tuple of characters in the string "Pythonforbeginners"
using the tuple()
function.
String to List of Characters Using List Comprehension
List comprehension is used to create a list from existing iterable objects in Python. As a string is an iterable object, we can create a list of characters from the string using list comprehension as shown below.
myStr="Pythonforbeginners"
print("The input string is:")
print(myStr)
character_list=[character for character in myStr]
print("The characters are:")
print(character_list)
Output:
The input string is:
Pythonforbeginners
The characters are:
['P', 'y', 't', 'h', 'o', 'n', 'f', 'o', 'r', 'b', 'e', 'g', 'i', 'n', 'n', 'e', 'r', 's']
String to List of Characters Using Unpacking Operator in Python
We use the python unpacking operator to unpack iterable objects and directly pack them into another iterable object.
To split a string into characters using the unpacking operator, we will first unpack the string using the * operator. Then, we will pack the characters into a list using python packing as shown in the following example.
myStr="Pythonforbeginners"
print("The input string is:")
print(myStr)
character_list=[*myStr]
print("The characters are:")
print(character_list)
Output:
The input string is:
Pythonforbeginners
The characters are:
['P', 'y', 't', 'h', 'o', 'n', 'f', 'o', 'r', 'b', 'e', 'g', 'i', 'n', 'n', 'e', 'r', 's']
In this example, the * operator unpacks the string "Pythonforbeginners"
into characters. Then, we packed the characters into a list using the square brackets.
String to List of Distinct Characters in Python
All the above-discussed approaches split the given string into characters whether duplicate or not. If you want to split the string into distinct characters in python, we will use the following approach.
- First, we will create an empty list named
character_list
to store the characters of the string. - Next, we will iterate through the characters of the input string using a for loop.
- While iteration, we will check if the current character is present in
character_list
or not. If yes, we will move to the next character. Otherwise, we will append the character tocharacter_list
using theappend()
method.
After execution of the for loop, we will get the characters of the string in the list character_list
. You can observe this in the following example.
myStr="Pythonforbeginners"
print("The input string is:")
print(myStr)
character_list=[]
for character in myStr:
if character not in character_list:
character_list.append(character)
print("The uniue characters are:")
print(character_list)
Output:
The input string is:
Pythonforbeginners
The uniue characters are:
['P', 'y', 't', 'h', 'o', 'n', 'f', 'r', 'b', 'e', 'g', 'i', 's']
In this example, you can observe that the output list only contains unique characters.
String to Set of Characters in Python
To split a string into distinct characters using a set, we will use the following steps.
- First, we will create an empty set named
character_set
to store the characters of the string. - Next, we will iterate through the characters of the input string using a for loop.
- While iteration, we will add the character to
character_set
using theadd()
method.
After execution of the for loop, we will get the characters of the string in the set character_set
. You can observe this in the following example.
myStr="Pythonforbeginners"
print("The input string is:")
print(myStr)
character_set=set()
for character in myStr:
if character not in character_set:
character_set.add(character)
print("The uniue characters are:")
print(character_set)
Output
The input string is: Pythonforbeginners The uniue characters are: {'h', 'n', 't', 'f', 'r', 'g', 's', 'i', 'y', 'b', 'P', 'o', 'e'}
In this example, we have created a set of characters using the for loop and the add()
method.
You can also use the update()
method to split a string into distinct characters. For this, you first need to create an empty set using the set()
function. Then, you can invoke the update()
method on the set and pass the string as its input argument.
After execution of the update()
method, you will get a set of distinct characters of the string as shown below.
myStr="Pythonforbeginners"
print("The input string is:")
print(myStr)
character_set=set()
character_set.update(myStr)
print("The uniue characters are:")
print(character_set)
Output:
The input string is:
Pythonforbeginners
The uniue characters are:
{'h', 'n', 't', 'f', 'r', 'g', 's', 'i', 'y', 'b', 'P', 'o', 'e'}
Instead of the above approaches, you can use the set()
function to obtain a set of distinct characters from the given string as shown below.
myStr="Pythonforbeginners"
print("The input string is:")
print(myStr)
character_set=set(myStr)
print("The uniue characters are:")
print(character_set)
Output:
The input string is:
Pythonforbeginners
The uniue characters are:
{'h', 'n', 't', 'f', 'r', 'g', 's', 'i', 'y', 'b', 'P', 'o', 'e'}
Conclusion
In this article, we have discussed different ways to split a string into characters in Python. To learn more about strings, you can read this article on string concatenation.
You might also like this article on how to drop columns from a pandas dataframe.
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.