Tutorial

Python Convert String to List

Published on August 4, 2022
author

Pankaj

Python Convert String to List

We can convert a string to list in Python using split() function. Python String split() function syntax is:

str.split(sep=None, maxsplit=-1)

Python Convert String to List

Let’s look at a simple example where we want to convert a string to list of words i.e. split it with the separator as white spaces.

s = 'Welcome To JournalDev'
print(f'List of Words ={s.split()}')

Output: List of Words =['Welcome', 'To', 'JournalDev']

If you are not familiar with f-prefixed string formatting, please read f-strings in Python

If we want to split a string to list based on whitespaces, then we don’t need to provide any separator to the split() function. Also, any leading and trailing whitespaces are trimmed before the string is split into a list of words. So the output will remain same for string s = ' Welcome To JournalDev ' too. Let’s look at another example where we have CSV data into a string and we will convert it to the list of items.

s = 'Apple,Mango,Banana'
print(f'List of Items in CSV ={s.split(",")}')

Output: List of Items in CSV =['Apple', 'Mango', 'Banana']

Python String to List of Characters

Python String is a sequence of characters. We can convert it to the list of characters using list() built-in function. When converting a string to list of characters, whitespaces are also treated as characters. Also, if there are leading and trailing whitespaces, they are part of the list elements too.

s = 'abc$ # 321 '

print(f'List of Characters ={list(s)}')

Output: List of Characters =['a', 'b', 'c', '$', ' ', '#', ' ', '3', '2', '1', ' '] If you don’t want the leading and trailing whitespaces to be part of the list, you can use strip() function before converting to the list.

s = ' abc '

print(f'List of Characters ={list(s.strip())}')

Output: List of Characters =['a', 'b', 'c'] That’s all for converting a string to list in Python programming.

You can checkout complete python script and more Python examples from our GitHub Repository.

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Learn more about our products

About the authors
Default avatar
Pankaj

author

While we believe that this content benefits our community, we have not yet thoroughly reviewed it. If you have any suggestions for improvements, please let us know by clicking the “report an issue“ button at the bottom of the tutorial.

Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?
 
JournalDev
DigitalOcean Employee
DigitalOcean Employee badge
September 26, 2020

“”" sometimes you do not to import anything to make it work “”" a = ‘test’ b = [] b += a print(b) > Output: [‘t’, ‘e’, ‘s’, ‘t’]

- _zippp

    Try DigitalOcean for free

    Click below to sign up and get $200 of credit to try our products over 60 days!

    Sign up

    Join the Tech Talk
    Success! Thank you! Please check your email for further details.

    Please complete your information!

    Become a contributor for community

    Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

    DigitalOcean Documentation

    Full documentation for every DigitalOcean product.

    Resources for startups and SMBs

    The Wave has everything you need to know about building a business, from raising funding to marketing your product.

    Get our newsletter

    Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.

    New accounts only. By submitting your email you agree to our Privacy Policy

    The developer cloud

    Scale up as you grow — whether you're running one virtual machine or ten thousand.

    Get started for free

    Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

    *This promotional offer applies to new accounts only.