Python – Split String

Python Split String

To split a String in Python using delimiter, you can use string split() method. split() method splits the given string by a specified separator, into a list of values.

In this tutorial, we shall learn about the syntax of string split() method, and how to use this method to split a given string into a list of values, with well detailed example programs.

Syntax of string split() method

The syntax of String.split() method is

str.split(sep, maxsplit)

where

  • str is the string which has to be split.
  • sep is the separator/delimiter where the string is split. If not provided, white spaces are considered as separators.
  • maxsplit is the maximum number of splits that can be done on the given string. If not provided, maximum possible splits are done.

Examples

1. Split given string by comma in Python

In this example, we are given a string where the values in the string are separated by comma. We have to split this string using comma as separator using string split() method.

Steps

  1. We are given a string value in variable x.
x = 'apple,banana,cherry,mango,fig'
  1. Call split() method on the string x, and pass the comma separator as argument.
x.split(',')
  1. The split() method returns a list containing chunks created from the splitting of the given string by specified separator. Store the returned list in values_list.
values_list = x.split(',')
  1. You may use values_list as required. In this example, we shall print it.

Program

The complete program to split the given string by comma separator using string split() method.

Python Program

# Given string
x = 'apple,banana,cherry,mango,fig'

# Split string by comma delimiter
values_list = x.split(',')

# Print split values
print(values_list)
Run Code Copy

Output

['apple', 'banana', 'cherry', 'mango', 'fig']

2. Split string with a constraint on number of splits in Python

In this example, we take a string that has chunks separated by comma. We will split this string using comma as separator and maximum number of split operations to 3.

Python Program

# Given string
x = 'apple,banana,cherry,mango,fig'

# Split string with number of splits constrained to 3
values_list = x.split(',', 3)

# Print split values
print(values_list)
Run Code Copy

When there are splits done at three locations, we get four chunks.

Output

['apple', 'banana', 'cherry', 'mango,fig']

3. Call split() on string with no arguments passed in Python

When no arguments are given to split() function, one or more white spaces are considered as delimiter, and the input string is split.

In this example, we will split a string by space using default behavior of string split() method.

Python Program

# Given string
x = 'apple banana cherry'

# Default behavior of split() method
values_list = x.split()

# Print split values
print(values_list)
Run Code Copy

Output

['apple', 'banana', 'cherry']

Summary

In this tutorial of Python Examples, we have gone through different scenarios where we split the string with different types of delimiters, controlled the number of splits, etc.

Related Tutorials

Code copied to clipboard successfully 👍