In this tutorial, we will look at how to convert a string in Python to a list with the help of some examples.
How to convert a string to a list?
There are two common use cases for converting a string to a list in Python –
📚 Discover Online Data Science Courses & Programs (Enroll for Free)
- Harvard University Data Science: Learn R Basics for Data Science
- Standford University Data Science: Introduction to Machine Learning
- UC Davis Data Science: Learn SQL Basics for Data Science
- IBM Data Science: Professional Certificate in Data Science
- IBM Data Analysis: Professional Certificate in Data Analytics
- Google Data Analysis: Professional Certificate in Data Analytics
- IBM Data Science: Professional Certificate in Python Data Science
- IBM Data Engineering Fundamentals: Python Basics for Data Science
- Harvard University Learning Python for Data Science: Introduction to Data Science with Python
- Harvard University Computer Science Courses: Using Python for Research
- IBM Python Data Science: Visualizing Data with Python
- DeepLearning.AI Data Science and Machine Learning: Deep Learning Specialization
- UC San Diego Data Science: Python for Data Science
- UC San Diego Data Science: Probability and Statistics in Data Science using Python
- Google Data Analysis: Professional Certificate in Advanced Data Analytics
- MIT Statistics and Data Science: Machine Learning with Python - from Linear Models to Deep Learning
- MIT Statistics and Data Science: MicroMasters® Program in Statistics and Data Science
🔎 Find Data Science Programs 👨💻 111,889 already enrolled
Disclaimer: Data Science Parichay is reader supported. When you purchase a course through a link on this site, we may earn a small commission at no additional cost to you. Earned commissions help support this website and its team of writers.
- Split the string by a delimiter (for example, a whitespace character) and get a list of strings resulting from the split.
- Split the string into a list of its constituent characters.
Let’s look at the above two use-cases in detail with the help of some examples.
List to string using string
You can use the string
split() function to get a list of strings resulting from splitting the original string on the occurrence of a delimiter.
Let’s look at an example –
# create a string s = "This is Sparta!" # list from string with split() ls = s.split() print(ls)
['This', 'is', 'Sparta!']
By default the
split() function splits the string by a whitespace character. You can see that we get the words in the above string in a list.
String to list using
You can use the Python built-in
list() function to get a list of the string characters from a string. Pass the string as an argument to the
Let’s look at an example. We will use the same string that we used in the example above to see the difference between the two methods and what they are intended for.
# create a string s = "This is Sparta!" # list from string with list() ls = list(s) print(ls)
['T', 'h', 'i', 's', ' ', 'i', 's', ' ', 'S', 'p', 'a', 'r', 't', 'a', '!']
The resulting list contains each string character as a separate element.
In this tutorial, we looked at two different methods to get a list from a string depending upon your specific use case.
- To split a string into a list of strings by a delimiter, use the string
- To get each character in the string as a list element, use the
You might also be interested in –
Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.