Sling Academy
Home/Python/Python: 4 Ways to Convert a String into a List

Python: 4 Ways to Convert a String into a List

Last updated: May 25, 2023

This concise, example-based article walks you through four different ways to turn a given string into a list in Python. Life is short, and time is precious; let’s get to the point.

Using the str.split() method

f you want to split a string into substrings based on a specific delimiter, you can use the str.split() method. This method returns a list of substrings. The separator can be empty, a space, a comma, or any other character that you want to use as a delimiter.

Example:

text = "Welcome to Sling Academy"

# split the text into words
words = text.split()
print(words)

Output:

['Welcome', 'to', 'Sling', 'Academy']

Using the list() function

The list() function can turn a string into a list whose each element is a character from the input string.

Example:

text = "ABCDEF"
characters = list(text)
print(characters)

Output:

['A', 'B', 'C', 'D', 'E', 'F']

Using list comprehension

List comprehension gives us a concise way to convert a string into a list by iterating over each character in the string.

Example:

my_string = "abcdefghijk"
my_list = [char for char in my_string]
print(my_list)

Output:

['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k']

Using a for…in loop

A for...in loop can be used to solve many tasks related to strings and lists in Python, including the task of converting a string into a list.

Example:

my_string = "abcdefghijk"
my_list = []

for letter in my_string:
    my_list.append(letter)

print(my_list)

Output:

['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k']

Next Article: Python: 4 Ways to Declare a Multiline String (Block String)

Previous Article: Python: Converting a string to lowercase/uppercase

Series: Working with Strings in Python

Python

You May Also Like

  • Python Warning: Secure coding is not enabled for restorable state
  • Python TypeError: write() argument must be str, not bytes
  • 4 ways to install Python modules on Windows without admin rights
  • Python TypeError: object of type ‘NoneType’ has no len()
  • Python: How to access command-line arguments (3 approaches)
  • Understanding ‘Never’ type in Python 3.11+ (5 examples)
  • Python: 3 Ways to Retrieve City/Country from IP Address
  • Using Type Aliases in Python: A Practical Guide (with Examples)
  • Python: Defining distinct types using NewType class
  • Using Optional Type in Python (explained with examples)
  • Python: How to Override Methods in Classes
  • Python: Define Generic Types for Lists of Nested Dictionaries
  • Python: Defining type for a list that can contain both numbers and strings
  • Using TypeGuard in Python (Python 3.10+)
  • Python: Using ‘NoReturn’ type with functions
  • Type Casting in Python: The Ultimate Guide (with Examples)
  • Python: Using type hints with class methods and properties
  • Python: Typing a function with default parameters
  • Python: Typing a function that can return multiple types