How do you split part of 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 separate part of a string?

Use the Split method when the substrings you want are separated by a known delimiting character (or characters). Regular expressions are useful when the string conforms to a fixed pattern. Use the IndexOf and Substring methods in conjunction when you don't want to extract all of the substrings in a string.

How do you cut half string in Python?

We can split the strings into two halves by using the slice () constructor. We separate the first half and second half of the string and then save these halves in different variables. To execute the codes in Python language, first install spyder software version 5.

How do you split and remove a part of a string in Python?

To split a string and remove whitespace:.
Use the str. split() method to split the string into a list..
Use a list comprehension to iterate over the list..
On each iteration, use the str. strip() method to remove the leading and trailing whitespace..

How do you split a string into 3 parts in Python?

The split() method returns a list of all the words in the string, using str as the separator (splits on all whitespace if left unspecified), optionally limiting the number of splits to num.