← Winnitude! Search  ·  Reader Mode (681 ms)
Links: Stay in Reader Retro Site Converter Retro Mode: OFF Original Page ↗
PROGRAMIZ · 78 LINKS

Python String split() | Programiz

Online Python Online JavaScript Online SQL Online Java Online HTML Online C Online C++ Online C# Online PHP Online Swift Online Kotlin Online TypeScript Online Go Online Rust Online Scala Online Dart Online R Online Ruby

Popular Tutorials

Getting Started With Python Python if Statement while Loop in Python Python Lists Dictionaries in Python Start Learning Python

Reference Materials

Built-in Functions List Methods Dictionary Methods String Methods View all

Certification Courses

Created with over a decade of experience and thousands of feedback.

Learn Python Learn HTML Learn JavaScript Learn SQL Learn DSA Learn C Learn C++ Learn Java View all Courses on

Popular Tutorials

Getting Started With Python Python if Statement while Loop in Python Python Lists Dictionaries in Python Start Learning Python All Python Tutorials

Reference Materials

Built-in Functions List Methods Dictionary Methods String Methods View all Python online compiler JavaScript online compiler SQL online compiler Java online compiler HTML online compiler C online compiler C++ online compiler C# online compiler PHP online compiler Swift online compiler Kotlin online compiler TypeScript online compiler Go online compiler Rust online compiler Scala online compiler Dart online compiler R online compiler Ruby online compiler

Python String split()

In this tutorial, we will learn about the Python String split() method with the help of examples.

The split() method breaks down a string into a list of substrings using a chosen separator.

Example

text = 'Python is fun'

# split the text from space
print(text.split())

# Output: ['Python', 'is', 'fun']

split() Syntax

str.split(separator, maxsplit)

split() Parameters

The split() method takes a maximum of 2 parameters:

  • separator (optional) - Specifies the delimiter used to split the string. If not provided, whitespace is used as the default delimiter.
  • maxsplit (optional) - Determines the maximum number of splits. If not provided, the default value is -1, which means there is no limit on the number of splits.

split() Return Value

The split() method returns a list of strings.


Example: Python String split()

text= 'Split this string'

# splits using space
print(text.split())

grocery = 'Milk, Chicken, Bread'

# splits using ,
print(grocery.split(', '))

# splits using :
# doesn't split as grocery doesn't have :
print(grocery.split(':'))

Output

['Split', 'this', 'string']
['Milk', 'Chicken', 'Bread']
['Milk, Chicken, Bread']

Here,

  • text.split() - splits the string into a list of substrings at each space character.
  • grocery.split(', ') - splits the string into a list of substrings at each comma and space character.
  • grocery.split(':') - since there are no colons in the string, split() does not split the string.

Example: split() with maxsplit

We can use the maxsplit parameter to limit the number of splits that can be performed on a string.

grocery = 'Milk#Chicken#Bread#Butter'

# maxsplit: 1
print(grocery.split('#', 1))

# maxsplit: 2
print(grocery.split('#', 2))

# maxsplit: 5
print(grocery.split('#', 5))

# maxsplit: 0
print(grocery.split('#', 0))

Output

['Milk', 'Chicken#Bread#Butter']
['Milk', 'Chicken', 'Bread#Butter']
['Milk', 'Chicken', 'Bread', 'Butter']
['Milk#Chicken#Bread#Butter']

Note : If maxsplit is specified, the list will have a maximum of maxsplit+1 items.


Also Read:

Share on: Did you find this article helpful? Previous Tutorial: Python String rindex() Next Tutorial: Python String rsplit()

Python References

Python Library

Python String rjust()

Python Library

Python String count()

Python Library

Python String replace()

Python Library

Python String isdigit()