FahmidasClassroom

Learn by easy steps

Fimage

Python has many built-in functions to do different types of string operations. The split() function is one of them. It is used to divide a string into multiple parts based on the particular separator value. The number of splits can also be defined by this function. The uses of this function have been shown in this tutorial by using multiple Python script examples.

Syntax:

The syntax of the split() function is given below.

string.split [ separator_value [, maxsplit]]

The split() function can take two argument values and both arguments are optional. The first argument is used as the separator value to divide a string in multiple parts. The second argument value is used to set the limit of the split. When the split() function is used without any argument then the space is used as the separator value.

The split() function returns a list of split values of the string.

Different examples of Python split() function:

Example-1: Use of split() without any argument

The way to split a string value based on the space by using the Python split() function has been shown in this example. The split() function has been used without any argument in the script.

#Take a string value from the user
input_val = input("Enter a string: ")
#Split the string based on the space
output_val = input_val.split()
#Print the string values after dividing based on the space
print ("The original string is :\n%s" %input_val)
print ("The divided values of the string is :\n%s" %output_val)

The following output will be generated if the input value is ‘Learn Python Programming’.

p1

Example-2: Use of split() with the separator value

The way to split a string value based on a separator value by using the Python split() function has been shown in this example. The split() function has been used with an argument in the script.

#Take the string value from the user
input_val = input("Enter the name of programming languages with comma: \n")
#Take the separator value from the user
sep_val = input("Enter the separator value: ")
#Split the string based on the separator
output_val = input_val.split(sep_val)
#Print the string values after dividing based on the space
print ("The original string is :\n%s" %input_val)
print ("The divided values of the string are :\n%s" %output_val)

A character, comma(,) has been used as a separator in the following output.

p2-1

A string, ‘Bash’ has been used as a separator in the following output.

p2-2

Example-3: Use of split() with two argument values

The way to split a string value based on a separator value and maximum split value by using the Python split() function has been shown in this example. The split() function has been used with two arguments in the script.

#Take the string value
input_val = input("Enter the string value: ")
#Take the separator value
sep_val = input("Enter the separator value: ")
#Take the max limit value
max_val = int (input("Enter the maxlimit value: "))
#Split the string based on the separator and max limit
output_val = input_val.split(sep_val, max_val)
#Print the string values after dividing based on the space
print ("The original string is :\n%s" %input_val)
print ("The divided values of the string are :\n%s" %output_val)

The following output will appear if the input value is, ‘First In First Out’, the separator value is ‘First’, and the maximum split value is 1.

p3

Example-4: Print the split values using the loop

The way to split a string value based on a separator value by using the Python split() function and print the values by using a loop have been shown in this example. The split() function returns a list as an output. So, a for-in loop has been used in the script to iterate the list.

#Take the string value
input_val = input("Enter the string value: ")
#Take the separator value
sep_val = input("Enter the separator value: ")
#Split the string based on the separator
output_val = input_val.split(sep_val)
#Print the string values after dividing based on the space
print ("The original string is :\n%s" %input_val)
print ("\nThe divided values of the string are:")
#Print the values by using the loop
for value in output_val:
print (value)

The following output will appear for the string value, ‘7856:Md. Alam:Manager:HR’, and the separator value, ‘:’.

p4

Conclusion:

Different ways of using the split() function in Python have been shown in this tutorial by using Python examples to know the uses of this function properly.