Python string split() Method

In this tutorial, We will see the python string split() method with examples.

split() Method

The split() method is one of the methods in a python string. The python string split() method is used to break a string.

  • The split() method returns a list of strings.
  • The output of the split() method is in the form of a list.
Syntax of Python string split() method: Using No Parameter
string.split()
  • In this case, The split() method will split it wherever there is a space in the string.
Example of split() Method: Using No parameter
info = "Cyber Fauz"

info1 = info.split()

print(info1)

#Output: ["Cyber","Fauz"]
Syntax of Python string split() method: Using 1 Parameter
string.split(separator)
  • Here separator is a parameter. The separator specifies on what basis you want to split your string.
  • If the separator is #, then the split method will split it wherever there is #  in the string. The separator parameter is optional.
Example of split() Method: Using 1 parameter

Split on the basis of #

Here separator is #, then the split() method will split it wherever there is # in the string.

info = "cyber#fauz#is#a#educational#site"

info1 = info.split("#")

print(info1)

#Output: ["cyber","fauz","is","a","educational","site"]

Split on the basis of comma (,)

Here separator is comma(,), then the split() method will split it wherever there is comma(,) in the string.

info = "cyber fauz,is,a,educational,site"

info1 = info.split(",")

print(info1)

#Output: 
['cyber fauz', 'is', 'a', 'educational', 'site']

Split on the basis of any particular word 

Here separator is a word, then the split() method will split it wherever there is specific word in the string.

info = "python is a programming language"

#split on the basis of a
info1 = info.split("a")

print(info1)

#Output: 
['python is ', ' progr', 'mming l', 'ngu', 'ge']
Syntax of Python string split() method: Using 2 Parameter
string.replace(separator,count)
  • Here 2 parameters are used. The separator parameter is the same as the previous syntax.
  • Here count is a parameter. The count parameter determines how many times the string will split If the separator occurs many times. Here count parameter is optional. By default, the value of the count is -1.
Example of split() Method: Using 2 parameter

Split on the basis of comma (,) only any specific no. of times

Here separator is a comma(,), then the split() method will split it wherever there is a comma(,) in the string only any specific no. of times.

info = "cyber fauz,is,a,educational,site"

#3 times split with comma(,)
info1 = info.split(",",3)

print(info1)

#Output: 
['cyber fauz', 'is', 'a', 'educational,site']

Split on the basis of any particular word only any specific no. of times

Here separator is a specific word, then the split() method will split it wherever there is a specific word in the string only any specific no. of times.

'''Split on the basis of is word only any specific no. of times'''

info = "Python is a programming language.Cyber fauz is a best platform to learn python."

#1 time split with is
info1 = info.split("is",1)

print(info1)

#Output: ['Python ', ' a programming language.Cyber fauz is a best platform to learn python.']
replace() Method Parameters
ParametersDescription
SeparatorThis parameter specifies on what basis you want to split your string. This parameter is optional.
CountDetermines how many times string will split. This parameter is also optional.
Related Program

Take a sentence from the user and check how many words are present in the sentence.

#user input

info = input("Enter the sentence")

info1 = info.split()

#length of the list, total words 

print(len(info1))

Related Posts