Split a string into a list of characters in Python

In this article, we will learn to split a given string into a list of characters using Python Programming language. Also we will learn about list and strings in python.

Table Of Contents

What we know about Strings and list ?

What is a String in Python?

A String is an array of bytes represented as Unicode characters, which are enclosed in single, double or triple quotes in Python. These enclosed characters can be either digit, alphabets or special symbols. A String is just normal text in human readable format. Also, strings in Python are immutable. It means once created, then they can not be changed.

What is a List in Python?

Advertisements

Lists are the built in data type of Python, that is mutable and stores data in ordered sequence. It stores multiple items in a single variable.
List is a heterogeneous data type. It means data stored in the list can be of type int, string, float or any other type.

Now we know about strings and list in Python. Let’s see how we can split a given string into a list of characters.

Split String to a list of characters using list() constructor.

First method we will be using to split a string into a list of characters, is by type casting the string into list using the list() constructor. The list() function takes only one parameter that is an object, here we will be taking a string object. It returns a list consisting of items which are in the passed as an argument. In our case, it will return a list of characters. Lets see an example below :

EXAMPLE :

strValue  = 'MSD is the best captain & finisher.'

# type() will print the data type of
# var strValue which is of class str.
print('Data type of var strValue is :', type(strValue))

# type casting using list() constructor
# to split into a list of characters.
listOfChars = list(strValue)

# type() will print the data type of var lst1 which is of class list.
print('Data type of var str2 is :',type(listOfChars))
print(listOfChars)

OUTPUT :

Data type of var strValue is : <class 'str'>
Data type of var listOfChars is : <class 'list'>
['M', 'S', 'D', ' ', 'i', 's', ' ', 't', 'h', 'e', ' ', 'b', 'e', 's', 't', ' ', 'c', 'a', 'p', 't', 'a', 'i', 'n', ' ', '&', ' ', 'f', 'i', 'n', 'i', 's', 'h', 'e', 'r', '.']

You can see in code and output above that the list() constructor has been used to split string of variable strValue in characters and stored in list variable listOfChars. Data type of variables in output verifies the above statement.

Split String to a list of characters using List comprehension

Next mehtod we will using to split strings into a list of chars is list comprehension. The List comprehension is a method with shorter syntax in Python, through which new list can be created based on given parameters or values. Lets see an example

EXAMPLE :

strValue  = 'MSD is the best captain & finisher.'

# type() will print the data type of 
# strValue var which is of class str.
print('Data type of var strValue is :',type(strValue))

# using list comprehension to split
#  string into list of chars.
listOfChars = [char for char in strValue]

# type() will print the data type of
# listOfChars var which is of class list.
print('Data type of var listOfChars is :', type(listOfChars))

print(listOfChars)

OUTPUT :

Data type of var strValue is : <class 'str'>
Data type of var listOfChars is : <class 'list'>
['M', 'S', 'D', ' ', 'i', 's', ' ', 't', 'h', 'e', ' ', 'b', 'e', 's', 't', ' ', 'c', 'a', 'p', 't', 'a', 'i', 'n', ' ', '&', ' ', 'f', 'i', 'n', 'i', 's', 'h', 'e', 'r', '.']

In code and output above of method 2, the list comprehension has been used to split string in var strValue into a list of characters in var listOfChars.

Summary

So in this article we learned about strings and list data type. Also we learned how can we split a string into a list of characters using two different methods which are by using list() constructor and by using combination of list comprehension and for loop. There are other methods like shelx.split() , string.split() through which we can split strings with given separators or by words. Here Type Casting (Method 1) is vey useful and has shorter syntax also it can be understood very easily.

Make sure to go through the article and always run these codes on your machines. We have used Python 3.10.1 for writing example codes. To check your version write python –version in your terminal.

Pandas Tutorials -Learn Data Analysis with Python

   

Are you looking to make a career in Data Science with Python?

Data Science is the future, and the future is here now. Data Scientists are now the most sought-after professionals today. To become a good Data Scientist or to make a career switch in Data Science one must possess the right skill set. We have curated a list of Best Professional Certificate in Data Science with Python. These courses will teach you the programming tools for Data Science like Pandas, NumPy, Matplotlib, Seaborn and how to use these libraries to implement Machine learning models.

Checkout the Detailed Review of Best Professional Certificate in Data Science with Python.

Remember, Data Science requires a lot of patience, persistence, and practice. So, start learning today.

Join a LinkedIn Community of Python Developers

Leave a Comment

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top