How do you split text in python?

❮ String Methods


Example

Split a string into a list where each word is a list item:

txt = "welcome to the jungle"

x = txt.split()

print(x)

Try it Yourself »


Definition and Usage

The split() method splits a string into a list.

You can specify the separator, default separator is any whitespace.

Note: When maxsplit is specified, the list will contain the specified number of elements plus one.


Syntax

string.split(separator, maxsplit)

Parameter Values

ParameterDescription
separator Optional. Specifies the separator to use when splitting the string. By default any whitespace is a separator
maxsplit Optional. Specifies how many splits to do. Default value is -1, which is "all occurrences"

More Examples

Example

Split the string, using comma, followed by a space, as a separator:

txt = "hello, my name is Peter, I am 26 years old"

x = txt.split(", ")

print(x)

Try it Yourself »

Example

Use a hash character as a separator:

txt = "apple#banana#cherry#orange"

x = txt.split("#")

print(x)

Try it Yourself »

Example

Split the string into a list with max 2 items:

txt = "apple#banana#cherry#orange"

# setting the maxsplit parameter to 1, will return a list with 2 elements!
x = txt.split("#", 1)

print(x)

Try it Yourself »


❮ String Methods


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

The split() method breaks up a string at the specified separator and returns a list of strings.

Example

text = 'Python is a fun programming language'

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

# Output: ['Python', 'is', 'a', 'fun', 'programming', 'language']

Syntax of String split()

The syntax of split() is:

str.split(separator, maxsplit)

split() Parameters

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

  • separator (optional)- Delimiter at which splits occur. If not provided, the string is splitted at whitespaces.
  • maxsplit (optional) - Maximum number of splits. If not provided, there is no limit on the number of splits.

split() Return Value

The split() method returns a list of strings.


Example 1: How split() works in Python?

text= 'Love thy neighbor'

# splits at space

print(text.split())

grocery = 'Milk, Chicken, Bread' # splits at ','

print(grocery.split(', '))

# Splits at ':' print(grocery.split(':'))

Output

['Love', 'thy', 'neighbor']
['Milk', 'Chicken', 'Bread']
['Milk, Chicken, Bread']

Example 2: How split() works when maxsplit is specified?

grocery = 'Milk, Chicken, Bread, Butter'

# maxsplit: 2

print(grocery.split(', ', 2))

# maxsplit: 1 print(grocery.split(', ', 1)) # 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']

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

Python String split() method in Python split a string into a list of strings after breaking the given string by the specified separator.

Python String split() Method Syntax

Syntax : str.split(separator, maxsplit)

Parameters :

  • separator : This is a delimiter. The string splits at this specified separator. If is not provided then any white space is a separator.
  • maxsplit : It is a number, which tells us to split the string into maximum of provided number of times. If it is not provided then the default is -1 that means there is no limit.

Returns : Returns a list of strings after breaking the given string by the specified separator.

Python String split() Method Example

Python3

string = "one,two,three"

words = string.split(',')

print(words)

Output:

['one', 'two', 'three']

Example 1: Example to demonstrate how split() function works

Here we are using the Python String split() function to split different Strings into a list, separated by different characters in each case.

Python3

text = 'geeks for geeks'

print(text.split())

word = 'geeks, for, geeks'

print(word.split(','))

word = 'geeks:for:geeks'

print(word.split(':'))

word = 'CatBatSatFatOr'

print(word.split('t'))

Output :

['geeks', 'for', 'geeks']
['geeks', ' for', ' geeks']
['geeks', 'for', 'geeks']
['Ca', 'Ba', 'Sa', 'Fa', 'Or']

Example 2: Example to demonstrate how split() function works when maxsplit is specified

The maxsplit parameter is used to control how many splits to return after the string is parsed. Even if there are multiple splits possible, it’ll only do maximum that number of splits as defined by maxsplit parameter.

Python3

word = 'geeks, for, geeks, pawan'

print(word.split(', ', 0))

print(word.split(', ', 4))

print(word.split(', ', 1))

Output :

['geeks, for, geeks, pawan']
['geeks', 'for', 'geeks', 'pawan']
['geeks', 'for, geeks, pawan']

How do you separate text in Python?

Python String split() Method The split() method splits a string into a list. You can specify the separator, default separator is any whitespace. Note: When maxsplit is specified, the list will contain the specified number of elements plus one.

How do you split a string into two in Python?

Use Split () Function This function splits the string into smaller sections. This is the opposite of merging many strings into one. The split () function contains two parameters.

How do you use the split function in Python?

How to use Split in Python.
Create an array. x = 'blue,red,green'.
Use the python split function and separator. x. split(“,”) – the comma is used as a separator. This will split the string into a string array when it finds a comma..
Result. ['blue', 'red', 'green'].

How do you split a string by character in Python?

Split strings in Python (delimiter, line break, regex, etc.).
Split by delimiter: split() Specify the delimiter: sep. ... .
Split from right by delimiter: rsplit().
Split by line break: splitlines().
Split by regex: re.split() ... .
Concatenate a list of strings..
Split based on the number of characters: slice..