Tutorial

Python String Split() Method

Published on April 17, 2024
Python String Split() Method

Python string split() function is used to split a string into the list of strings based on a delimiter.

Python string split() function syntax

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

sep argument is used as the delimiter. If the string contains consecutive delimiters, then an empty string is returned. Delimiter argument can be of multiple characters too.

If the delimiter is not provided or None, then whitespaces are considered as the delimiter. In this case, no empty string will be returned in case there are leading or trailing white spaces. Also, multiple whitespaces will be considered as a single delimiter.

If maxsplit is provided, at most maxsplit splits are done (thus, the list will have at most maxsplit+1 elements). If maxsplit is not specified or -1, then there is no limit on the number of splits and all possible splits are returned in the list.

Python String split() example

Let’s look at a simple example where a string will be split into a list based on the specified delimiter.

s = 'Python is Nice'

# simple string split example
str_list = s.split(sep=' ')
print(str_list)

Output:

['Python', 'is', 'Nice']

String split() with maxsplit example

s = 'Python is Nice'

str_list = s.split(sep=' ', maxsplit=1)
print(str_list)

Output: ['Python', 'is Nice']

Notice that returned list has only 2 items, the string was split only once.

sep is not provided or None

s = '  Java  Python iOS    Android  '
str_list = s.split()
print(str_list)

Output: ['Java', 'Python', 'iOS', 'Android']

The leading and trailing whitespaces are ignored in the returned list. Also, consecutive whitespaces are also considered as a single delimiter.

Multiline string split example

multiline_str = 'Hi There\nHow are you?\nI am fine'
multiline_str_split_list = multiline_str.split(sep='\n')
for s in multiline_str_split_list:
    print(s)

Output:

Hi There
How are you?
I am fine

Multi-Character separator example

s = 'Hi||Hello||Adios'
str_list = s.split('||')
print(str_list)

Output: ['Hi', 'Hello', 'Adios']

str.split() function example

We can use split() function directly from str class too.

print(str.split('ABACAD', sep='A'))
print(str.split('ABACAD', sep='A', maxsplit=2))

Output:

['', 'B', 'C', 'D']
['', 'B', 'CAD']

Notice that empty string is returned when the first character matches the separator.

CSV String Split Example with User Input

Finally, let’s look at a real-life example where the user will enter the CSV data and we will split it into the list of strings.

input_csv = input('Please enter CSV Data\n')
input_csv_split_list = input_csv.split(sep=',')
print('Input Data Length =', len(input_csv_split_list))
print('List of inputs =', input_csv_split_list)

Output:

Please enter CSV Data
Java,Android,Python,iOS,jQuery
Input Data Length = 5
List of inputs = ['Java', 'Android', 'Python', 'iOS', 'jQuery']

python string split

That’s all for python string split() function examples. It’s a very useful function to split a string into the list based on some delimiter.

Python String rsplit()

Python string rsplit() function is very similar to split() function. The only difference is that the splits are done starting at the end of the string and working to the front.

Let’s look at some of the rsplit() function examples.

# rsplit() example
s = 'Python is Awesome'
str_list = s.rsplit(sep=' ')
print(str_list)

str_list = s.rsplit(sep=' ', maxsplit=1)
print(str_list)

s = '  Java  Python iOS    Android  '
str_list = s.rsplit()
print(str_list)

multiline_str = 'Hi There\nHow are you?\nI am fine'
multiline_str_split_list = multiline_str.rsplit(sep='\n')
for s in multiline_str_split_list:
    print(s)

s = 'Hi||Hello||Adios'
str_list = s.rsplit('||')
print(str_list)

# using split() with str class
print(str.rsplit('ABACAD', sep='A'))
print(str.rsplit('ABACAD', sep='A', maxsplit=2))

# csv and user input example
input_csv = input('Please enter CSV Data\n')
input_csv_split_list = input_csv.rsplit(sep=',')
print('Input Data Length =', len(input_csv_split_list))
print('List of inputs =', input_csv_split_list)

Output:

['Python', 'is', 'Awesome']
['Python is', 'Awesome']
['Java', 'Python', 'iOS', 'Android']
Hi There
How are you?
I am fine
['Hi', 'Hello', 'Adios']
['', 'B', 'C', 'D']
['AB', 'C', 'D']
Please enter CSV Data
x,y,z
Input Data Length = 3
List of inputs = ['x', 'y', 'z']

Notice that the difference is visible when maxsplit argument is provided. Otherwise, the split() and rsplit() function output is same.

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

Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?
 
1 Comments


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

This comment has been deleted

    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.