• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / Python / Convert String to List Using Python

Convert String to List Using Python

August 19, 2022 Leave a Comment

To convert a string into a list using Python, the easiest way is with the list() function.

string = "string"

l = list(string)

print(l)

#Output:
['s', 't', 'r', 'i', 'n', 'g']

If you want to convert a string into a list of elements with a delimiter, you can use the split() function.

string = "this is a string with spaces"

l = string.split()

print(l)

#Output:
['this', 'is', 'a', 'string', 'with', 'spaces']

Another way you can convert a string into a list is with comprehension.

string = "string"

l = [char for char in string]

print(l)

#Output:
['s', 't', 'r', 'i', 'n', 'g']

When working with different objects in Python, the ability to be able convert objects into other objects easily can be useful.

One such case is if you want to convert a string into a list in Python.

To convert a string into a list using Python, the easiest way is with the list() function.

list() creates a new list with elements containing each character of the string.

Below is a simple example showing you how to convert a string to a list in Python.

string = "string"

l = list(string)

print(l)

#Output:
['s', 't', 'r', 'i', 'n', 'g']

Using split() to Split String by Delimiter into List Using Python

A very useful function in Python is the split() function.

By default, split() converts a string into a list where each item is the words of the string.

You can also use split() to split a string by a different delimiter.

With split(), you can convert a string into a list.

For example, if you wanted to split a list by the spaces, then you can use split() in the following way.

string = "this is a string with spaces"

l = string.split()

print(l)

#Output:
['this', 'is', 'a', 'string', 'with', 'spaces']

Using Comprehension to Convert String to List in Python

One other way you can convert a string to a list is with comprehension.

With comprehension, you can loop over each character in a string and add it to a list.

Below is how you can use comprehension to convert a string into a list of the characters in Python.

string = "string"

l = [char for char in string]

print(l)

#Output:
['s', 't', 'r', 'i', 'n', 'g']

Hopefully this article has been useful for you to learn how to convert strings to lists in Python.

Other Articles You'll Also Like:

  • 1.  Convert Integer to Bytes in Python
  • 2.  Fibonacci Sequence in Python with for Loop
  • 3.  Using Python to Count Number of True in List
  • 4.  Python cosh – Find Hyperbolic Cosine of Number Using math.cosh()
  • 5.  Get First Character of String in Python
  • 6.  Get Python Dictionary Values as List
  • 7.  Check if Variable is String in Python
  • 8.  Count Primes Python – How to Count Number of Primes in a List
  • 9.  Using Python to Return Two Values from Function
  • 10.  Negate Boolean in Python with not Operator

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy