Python Principles

Converting a string to a list

This post explains how to convert a string to a list in Python.

List of words

If you need a list of words in the given string, use the built-in .split() method on the string. For example:

>>> "this is a test".split()
['this', 'is', 'a', 'test']

If the words are separated by a specific character, you can give that character as an argument to split:

>>> "yet,another,example".split(",")
['yet', 'another', 'example']

If you need to do something with each word, use a for loop like this:

>>> sentence = "this is an example"
>>> for word in sentence.split():
...     print(word)

We have written more about how to split a string in a different post.

List of letters

Sometimes, you need a list where each element is a letter from your string. In this case, use the list() built-in function:

>>> list("abc")
['a', 'b', 'c']

Improve your Python skills fast

The fastest way to learn programming is with lots of practice. Learn a programming concept, then write code to test your understanding and make it stick. Try our online interactive Python course today—it's free!

Learn more about the course

Want to get better at Python quickly? Try our interactive lessons today! Memberships are 100% FREE this week only!