Python Strings Functionsplit()
Defining the splite() function in Python
Returns a copy of the invoked script divided as a string array.
The location of the parameter We seppass a text that specifies the way the text is divided and each section is placed in a single element within the array.
Build the splite() function in Python
str.split(sep=None, maxsplit=-1)
Parameters of the splite() function in Python
Parameter location
sepWe pass the text on the basis of which the text that called it will be hashed.Parameter location
maxsplitWe can not pass a value and we can pass a regular number if we want to specify the maximum number of elements in the array that the function will return.
Return value of the splite() function in Python
Returns a copy of the invoked script divided as a string array.
first example
# s Here we have defined a text variable named
s = 'split method is very useful.'
# Based on the empty spaces s to split the text in the variable split() here we called the function
# arr and thus each word in the text will be placed in an element in the array
arr = s.split(' ')
# Note that each element contains one word .arr Here we print the text that is divided by 5 elements in the array
print(arr[0])
print(arr[1])
print(arr[2])
print(arr[3])
print(arr[4])
• We will get the following result when running.
method
is
very
useful.
second example
# s Here we have defined a text variable named s = 'split method is very useful.' # Based on the empty spaces s to split the text in the variable split() here we call the function # arr and thus each word in the text will be placed in an element in the array arr = s. for element in arr: print (element)
• We will get the following result when running.
method
is
very
useful.
Python Strings Functionsplitlines
Defining the splitlines() function in Python
Returns a copy of the invoked script divided as a string array. Each element in this array is a line in the text.
Build the splitlines() function in Python
str.splitlines([keepends])
Parameters of the splitlines() function in Python
Parameter location keependsYou can pass the value Trueto hold the symbols with which the Python compiler knows how to separate lines and then add each line as an element in the array.
Return value of the splitlines() function in Python
Returns a copy of the invoked script divided as a string array. Each element in this array is a line in the text.
first example
# consists of three lines s Here we have defined a text variable named s = 'This is fist line.\nThis is second line.\nThis is third line.' # Based on the end of line s code to split the text in the variable splitlines() here we call the function # arr and thus each line of text will be placed in an element in the array arr = s.splitlines() # Note that each element contains a line one .arr Here we print the text that is divided by 3 elements in the array print(arr[0]) print(arr[1]) print(arr[2])
• We will get the following result when running.
This is second line.
This is the third line.
second example
# consists of three lines s Here we have defined a text variable named s = 'This is fist line.\nThis is second line.\nThis is third line.' # On the basis of the symbol that indicates the end of the line s to split the text in the variable splitlines() here we called the function # arr and thus each line of text will be placed in an element in the array arr = s.splitlines() # for using the arr loop Here we show Array values for element in arr: print(element)
• We will get the following result when running.
This is second line.
This is the third line.