How to use Split Function in Python

How to use Split Function in Python

Split Function in Python

In python we have used different data types is that one of the types is String. The string is considered it is immutable. i.e you cannot able to manipulate the string. These kinds of split function will help to manipulate. This split function helps to breaks the string into substrings i.e array of small strings. Before learning about split first we need knowledge about String.

String

Normally string is a sequence of character values. It is also called a sequence of Unicode characters. Why its called Unicode means we can any values like characters as well as numbers in a string. It is also immutable in nature. The string will represent either a single quote or double quotes in python. Because here there is no character data type.

Example:

A=”Hello”
B=’world’
print(A)
print(B)

Need for split

The split function is used to break the string into a list of strings. It has some advantages also

  • It becomes easy to analyze the strings for deductions.
  • It is used to divide the larger string into smaller parts.
  • Used for encrypt the strings.

Syntax:

split(separator,max split)

separator – It is another string used for splitting the string. It is also considered as a delimiter for splitting the string. The default is space. i.e you are not passing any parameter it will split the string using space.

max split – It is a numeric value to indicates the number of strings we need after the split. i.e split will return the list of strings on that we fix the length of the list. If you given -1 means no limit for splitting. It is an optional parameter.

Example:

s=”This is python”
s1=s.split()
print(s1)

In this example, we use the default delimiter space to split the string into list of strings. Here we also not mention the max split also.

Output:

[‘This’, ‘is’, ‘python’]

Example:

s=”Red ball,blue ball,green ball”
s1=s.split(‘,’)
print(s1)

In this example we use the delimiter string as ‘,’ to split the string into list of strings. Here instead of space we use ‘,’. Also we are not given maxsplit.

Output:

[‘Red ball’, ‘blue ball’, ‘green ball’]

Using Max split parameter

Example:

s=”Red ball,blue ball,green ball”
s2=s.split(‘,’,1)
print(s2)

In this example, we use the max split parameter as 1 with a delimiter of ‘,’. Normally it will split the strings into a number of strings in the previous examples. But here we restrict the split into only 2 strings. i.e max split value is 1 means 0 and 1. So that split method divided the string into only two strings in a list. Normally it’s 3.

Output:

[‘Red ball’, ‘blue ball,green ball’]

Example:

s=’antbeecatdog’
print([s[i:i+3] for i in range(0,len(s),3)])

In this example we are split the strings without using split method. Here we are not using delimeter also to split. Just use the logic with slicing to split the string into list of strings it is also the way to split string.

Output:

[‘ant’, ‘bee’, ‘cat’, ‘dog’]

Related Blogs

Leave a Reply

Your email address will not be published. Required fields are marked *

Looking for Online Training