Python String split() Method Tutorial

In this section, we will learn what String split() method is and how to use it in Python.

How to Split a String in Python? (Parsing a String in Python)

The Python string split() method is used to split a string value into sub-string and return those sub-string in a list as a result.

Python String split() Method Syntax:

string.split(separator, maxsplit)

String split() Method Parameter

The method takes two arguments:

  • The first argument is the separator that will be used to split the target string. By default, this value is set to a white space. That means anywhere in the string that there’s a white space of any kind (newline or tab etc.) that will be the point where the string splits. But we can change this value to other characters.
  • The second argument is the number of times that the string should split. The default value is set to -1 and that means split the string as long as there’s a separator in the string that could be used for this purpose. But if you change this value to something like 2, that means only split the target string 2 times.

String split() Method Return Value

The return value of this method is a reference to a list object that contains the sub-values of the target string as a result of invoking this method on it.

Example: python split string into list

s1 = "My name is John Doe"



['My', 'name', 'is', 'John', 'Doe']

Example: python split string by character

s1 = "My name is John Doe"



['My ', 'ame is Joh', ' Doe']

Example: splitting strings in python

s1 = "My name is John Doe \n I'm 1000 years old! "



['My name is John Doe ', " I'm 1000 years old! "]

Note: this last example could be done with the help of splitlines() method as well.


Top Technologies