How do you split a string in python?

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 split a string into multiple strings in Python?

Split String in Python. To split a String in Python with a delimiter, use split() function. split() function splits the string into substrings and returns them as an array.

How do you split strings?

Java String split() method with regex and length example.
public class SplitExample2{.
public static void main(String args[]){.
String s1="welcome to split world";.
System.out.println("returning words:");.
for(String w:s1.split("\\s",0)){.
System.out.println(w);.
System.out.println("returning words:");.

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 word in Python?

Use the list() class to split a word into a list of letters, e.g. my_list = list(my_str) . The list() class will convert the string into a list of letters.