Python String split()

The split() method in python splits a string into a list based on the specified separator. The separator can be specified, however, the default separator is any whitespace.

Syntax:

string.split(separator, maxsplit)

Parameters

  • separator: This is optional. The separator to be used when separating the string. By default, any whitespace serves as a separator.
  • maxsplit: This is optional. It represents the number of splits to perform. The default value is -1, which represents “all occurrences.”

Return Value:

A list of strings is returned by the split() method

NOTE

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

Python String split()

Method #1: Using split() Function (Static Input)

Approach:

  • Give the first string as static input and store it in a variable
  • Apply split() function on the given first string and print the result.
  • Here it splits the string based on the white spaces as there is no separator argument passed.
  • Give the second string as static input and store it in another variable
  • Apply split() function on the given second string by passing the separator as an argument to it and print the result.
  • Here it splits the string based in the ‘@’ symbol
  • Similarly split based on other separators and print the result.
  • The Exit of the Program.

Below is the implementation:

# Give the first string as static input and store it in a variable
gvnstring_1 = 'Hello this is pythonprograms'

# Apply split() fuction on the given first string and print the result. 
# Here it splits the string based on the white spaces as there 
# is no separator argument passed.
print("Splitting gvnstring_1 based on spaces:", gvnstring_1.split())

# Give the second string as static input and store it in another variable
gvnstring_2 = 'Welcome@to@pythonprograms'

# Apply split() function on the given second string by 
# passing the separator as an argument to it and print the result. 
# Here it splits the string based in the '@' symbol
print("Splitting gvnstring_2 based on '@' symbol:", gvnstring_2.split('@'))

# Similary split based on other separators
gvnstring_3 = 'Welcome:to:pythonprograms'
print("Splitting gvnstring_3 based on ':' symbol:", gvnstring_3.split(':'))

Output:

Splitting gvnstring_1 based on spaces: ['Hello', 'this', 'is', 'pythonprograms']
Splitting gvnstring_2 based on '@' symbol: ['Welcome', 'to', 'pythonprograms']
Splitting gvnstring_3 based on ':' symbol: ['Welcome', 'to', 'pythonprograms']

Method #2: When maxsplit argument is passed (Static Input)

Approach:

  • Give the first string as static input and store it in a variable
  • Apply split() fuction on the given first string by passing space, maxsplit = 0 as arguments to it and print the result.
  • Here it splits the string based on the white spaces but max split is given as 0. So it doesn’t split.
  • Give the second string as static input and store it in another variable
  • Apply split() fuction on the given second string by passing the separator as ‘@’ and maxsplit = 2 as arguments to it and print the result.
  • Here 2 specifies we are going to split two times if the @ symbol is present.
  • Similarly split based on other separators.
  • The Exit of the Program.

Below is the implementation:

# Give the first string as static input and store it in a variable
gvnstring_1 = 'Hello this is btechgeeks'

# Apply split() fuction on the given first string by passing space,
# maxsplit = 0 as arguments to it and print the result. 
# Here it splits the string based on the white spaces but max split is given as 0 
# So it doesn't split
print("Splitting gvnstring_1 based on spaces and maxsplit = 0:")
print(gvnstring_1.split(' ', 0))

# Give the second string as static input and store it in another variable
gvnstring_2 = 'Welcome to@btechgeeks'

# Apply split() fuction on the given second string by passing the separator as '@' and 
# maxsplit = 2 as arguments to it and print the result. 
# Here 2 specifies we are going to split two times if the @ symbol is present
print("Splitting gvnstring_2 based on '@' symbol and maxsplit = 2:")
print(gvnstring_2.split('@', 2))

# Similary split based on other separators
gvnstring_3 = 'Welcome:to:btechgeeks'
print("Splitting gvnstring_3 based on ':' symbol and maxsplit = 1:")
print(gvnstring_3.split(':', 1))

Output:

Splitting gvnstring_1 based on spaces and maxsplit = 0:
['Hello this is btechgeeks']
Splitting gvnstring_2 based on '@' symbol and maxsplit = 2:
['Welcome to', 'btechgeeks']
Splitting gvnstring_3 based on ':' symbol and maxsplit = 1:
['Welcome', 'to:btechgeeks']

Method #3: Using split() Functions (User Input)

Approach:

  • Give the first string as user input using the input() function and store it in a variable.
  • Give the second string as user input using the input() function and store it in another variable.
  • Apply split() function on the given string by passing the given separator as an argument to it and print the result.
  • Here it splits the string based in the given separator.
  • The Exit of the Program.

Below is the implementation:

# Give the string as user input using the input() function 
# and store it in a variable
gvn_str = input("Enter some random string = ")

# Give the string as user input using the input() function 
# and store it in another variable
gvn_separator = input("Enter some random separator = ")

# Apply split() function on the given string by passing the given separator 
# as an argument to it and print the result. 
# Here it splits the string based in the given separator 
print("Splitting the given string {",gvn_str,"} based on the given separator{",gvn_separator,"}:")
print(gvn_str.split(gvn_separator))

Output:

Enter some random string = hello&this&is&btechgeeks
Enter some random separator = &
Splitting the given string { hello&this&is&btechgeeks } based on the given separator{ & }:
['hello', 'this', 'is', 'btechgeeks']

Method #4: When maxsplit argument is passed (User Input)

# Give the string as user input using the input() function and store it in a variable
gvn_string= 'Hello#this#is#pythonprograms'

# Give the separator value as user input using the input() function 
# and store it in another variable
gvn_separator = input("Give some random separator = ")

# Give the maxsplit value as user input using the int(), input() function 
# and store it in another variable
gvn_maxsplit = int(input("Give some random maxsplit value = "))

# Apply split() function on the given string by passing the given separator 
# and maxsplit values as arguments to it to split the string based on the
# given separator and maxsplit values
print("Splitting the given string based on", gvn_separator,"and maxsplit =",gvn_maxsplit,":")
print(gvn_string.split(gvn_separator, gvn_maxsplit))

Output:

Give some random separator = #
Give some random maxsplit value = 2
Splitting the given string based on # and maxsplit = 2 :
['Hello', 'this', 'is#pythonprograms']