Splitting a String in Python

There are at least five ways to split a string in Python. The simplest and most commonly used method is a string.split(), which by default splits a Python string into a list of substrings using spaces, tabs, and line breaks. Other popular options for splitting strings are the string.splitlines() and Regular Expressions. In some rare cases, you can split a Python string using the range operator [] to take a subrange of characters from the string. In this Python Split String example, we use the string.split() method to split the string into a list. Other splitting options are presented below, with detailed examples and a description of each. Click Execute to run the Python Split String Example online and see the result.
Splitting a String in Python Execute
my_string = 'I am a Python string.'
result = my_string.split()

print(result)
Updated: Viewed: 8272 times

What is a string in Python?

In Python, a string is an array of 16-bit Unicode bytes (and 8-bit ANSI bytes for Python 2), where each string character is denoted by one byte. In Python, a single character is also a string of length 1. The square brackets "[]" can be used to access the characters in the string. Python strings are immutable, which means that once they are created, they can no longer be changed. All string processing methods return a copy of the string and do not modify the original. String literals can be enclosed in single, double, or triple quotes. You can use single quotes in double and triple quoted strings and vice versa.

The built-in Python "str" library provides essential methods out of the box for searching, concatenating, reversing, splitting, comparing strings, and more.

Splitting a Python string using the string.split() method

The easiest and most common way to split a string is to use the string.split(separator, maxsplit) method. By default, the string.split() method breaks a line by spaces, tabs, and line breaks. To split a string by another character, or even another string, you can pass the delimiter as a parameter to the string.split() method.

Split string with string.split()
print('I am Python string.'.split())

# ['I', 'am', 'Python', 'string.']

An example of splitting a string at a custom separator:

Split string at custom separator
print('python-string-example'.split('-'))

# ['python', 'string', 'example']

An example of splitting a string using a substring as a delimiter:

Split string at a substring
print('I am a Python string'.split('Python'))

# ['I am a ', ' string']

Python string.split() method syntax

The string.split() method accepts two parameters.

Python string.split() syntax
string.split(separator, maxsplit)

Where:
  • separator (optional) - the delimiter by which the line will be split. If no separator is specified, then string.split() method will split the line at using spaces, tabs, and line breaks.
  • maxsplit (optional) - the maximum number of splits that the string.split() method will perform. If maxsplit is not specified, the number of splits is unlimited. The default is -1, which means that the string.split() method will split the string into all occurrences of the separator.

An example of limiting the number of splits by passing the maxsplit parameter to the string.split() method.

Limiting the number of splits
print('Python string example'.split(' ', 1))

# ['Python', 'string example']

Splitting Python string in reverse order (right to left)

The string.rsplit() pair method is similar to the string.rsplit() method and has the same signature, but splits the string from right to left if the maxsplit parameter is specified.

Split string in reverse order
print('Python string example'.rsplit(' ', 1))

# ['Python string', 'example']

Splitting a string at line breaks using the string.splitlines() method

The Python splitlines() method splits a text at line breaks. Line breakers can be one of the following characters: "\n", "\r", "\r\n", "\v", or "\f".

Split string at line breaks
print('one\ntwo\nthree'.splitlines())

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

Python string.splitlines() method syntax

The string.splitlines() method accepts one parameter.

Python string.splitlines() syntax
string.splitlines(keeplinebreaks)

Where:
  • keeplinebreaks (optional) - specifies whether line breaks should be included (True) or not (False) in list items (default is False).
Preserving line breaks when splitting a string
print('one\ntwo\nthree'.splitlines(True))
        
# output: ['one\n', 'two\n', 'three']

Splitting Python strings using Regular Expressions

Besides using the string.split() and string.splitlines() methods, you can also split strings using the Python Regex library. To do this, you first need to import the re library and call re.split(), passing a regular expression as the first parameter and the string itself to be split as the second parameter.

Split Python string using regex
import re
print(re.split('[_^#]', 'one#two_three^four'))

# ['one', 'two', 'three', 'four']

Regular expressions are more challenging to use and maintain, but they provide more flexibility for splitting strings based on complex conditions.

Splitting Python string using the range operator

Since strings in Python are an array of bytes, you can use the range operator to take a range of characters from a string, just like you do for collections.

Split Python string by range
print('Python string'[2:10])

# thon str

Conclusion

Python provides an essential set of methods for splitting strings. In most cases, it will be sufficient to use the string.split() and string.splitlines() methods to split a string at spaces, tabs, and line breaks. For complex conditions, you can use regular expressions, and in some simple cases, it may be sufficient to take a range of characters from a string using the range operator.

See Also