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

  • Introduction to yfinance: Fetching Historical Stock Data in Python
  • Monitoring Volatility and Daily Averages Using cryptocompare
  • Advanced DOM Interactions: XPath and CSS Selectors in Playwright (Python)
  • Automating Strategy Updates and Version Control in freqtrade
  • Setting Up a freqtrade Dashboard for Real-Time Monitoring
  • Deploying freqtrade on a Cloud Server or Docker Environment
  • Optimizing Strategy Parameters with freqtrade’s Hyperopt
  • Risk Management: Setting Stop Loss, Trailing Stops, and ROI in freqtrade
  • Integrating freqtrade with TA-Lib and pandas-ta Indicators
  • Handling Multiple Pairs and Portfolios with freqtrade
  • Using freqtrade’s Backtesting and Hyperopt Modules
  • Developing Custom Trading Strategies for freqtrade
  • Debugging Common freqtrade Errors: Exchange Connectivity and More
  • Configuring freqtrade Bot Settings and Strategy Parameters
  • Installing freqtrade for Automated Crypto Trading in Python
  • Scaling cryptofeed for High-Frequency Trading Environments
  • Building a Real-Time Market Dashboard Using cryptofeed in Python
  • Customizing cryptofeed Callbacks for Advanced Market Insights
  • Integrating cryptofeed into Automated Trading Bots