Python String split() Method

By Lenin Mishra

If you prefer to watch Youtube videos over reading blogs, check out our video on Python strings here.

Python String split() method

The split() method in Python splits a string into a list, starting from the left.

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

Syntax of split() method
string.split(separator, maxsplit)

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

Code/Output

str1 = "Pylenin loves Python"
print(str1.split())
>>> ['Pylenin', 'loves', 'Python']

# Using the maxsplit parameter
str1 = "Pylenin loves Python"
print(str1.split(" ", 1))
>>> ['Pylenin', 'loves Python']

str1 = "Pylenin loves Python"
print(str1.split("loves", 1))
>>> ['Pylenin ', ' Python']

Check out other commonly used Python string methods.

  1. How to create a string in Python?
  2. How to access characters in a Python string?
  3. How to replace characters in a string in Python?
  4. How to concatenate strings in Python?
  5. How to iterate through a string in Python?
  6. Check if a Substring is Present in a Given String in Python
  7. Escape sequences in Python String
  8. Python String Formatting - The Definitive Guide