Advertisement
count(value, start, end)
Parameters | Description |
---|---|
separator | This optional parameter is a string value, which acts a separator, used to split the values of the invoked string. The default separator is white space. |
maxnumber | This optional parameter is an integer value, used to specify the maximum number of splits to perform. Its default value is -1 i.e. a split for each occurrence of separator value. |
# Python - Example of split() method.
# Creating a string
s1 = 'Do not rest yet, get up and work hard!'
# Printing the value of string
print('String is : ', s1)
# Calling the split() method on string
# which will perform the number of splits on the basis of default separator i.e. white space
# and returns a list containing all the split values
a_list = s1.split()
# Priting all the split values
print('A list splits : ', a_list)
String is : Do not rest yet, get up and work hard!
A list of splits ['Do', 'not', 'rest', 'yet,', 'get', 'up', 'and', 'work', 'hard!']
Advertisement
# Python - Example of split() method.
# Creating a string
s1 = 'Tomorrow never comes, All we have is NOW!'
# Printing the value of string
print('String is : ', s1)
# Calling the split() method on string
# which will perform the number of splits on the basis of separator upper case 'A'
a_list = s1.split('A')
# Priting all the spiits
print('Number of splits when the separator is A : ', a_list)
# Calling the split() method on string
# which will perform the number of splits on the basis of separator lower case 'e'
# used by the split() method i.e. white space
a_list = s1.split('e')
# Priting all the spiits
print('Number of splits when the separator is e :', a_list)
# Calling the split() method on string
# which will perform the number of splits on the basis of separator ','
# used by the split() method i.e. white space
a_list = s1.split(',')
# Priting all the spiits
print('Number of splits when the separator is , :', a_list)
# Calling the split() method on string
# which will perform the number of splits on the basis of separator lower case 'n'
# used by the split() method i.e. white space
a_list = s1.split('n')
# Priting all the spiits
print('Number of splits when the separator is n :', a_list)
# Calling the split() method on string
# which will perform the number of splits on the basis of separator lower case 'N'
# used by the split() method i.e. white space
a_list = s1.split('N')
# Priting all the spiits
print('Number of splits when the separator is N :', a_list)
String is : Tomorrow never comes, All we have is NOW!
Number of splits when the separator is A : ['Tomorrow never comes, ', 'll we have is NOW!']
Number of splits when the separator is e : ['Tomorrow n', 'v', 'r com', 's, All w', ' hav', ' is NOW!']
Number of splits when the separator is , : ['Tomorrow never comes', ' All we have is NOW!']
Number of splits when the separator is n : ['Tomorrow ', 'ever comes, All we have is NOW!']
Number of splits when the separator is N : ['Tomorrow never comes, All we have is ', 'OW!']
# Python - Example of rsplit() method.
# Creating a string
s1 = 'Eddie edited it'
# Printing the value of string
print('String is : ', s1)
# Calling the rsplit() method on string
# which will perform 2 splits on the basis of separator lower case 'e'
a_list = s1.split('e', 2)
# Priting all the spiits
print('2 Splits when the separator is e : ', a_list)
# Calling the rsplit() method on string
# which will perform 3 splits on the basis of separator lower case 'i'
a_list = s1.split('i', 3)
# Priting all the spiits
print('3 Splits when the separator is i :', a_list)
# Calling the rsplit() method on string
# which will perform 1 split on the basis of separator lower case 't'
a_list = s1.split('t', 1)
# Priting all the spiits
print('1 split when the separator is t :', a_list)
# Calling the rsplit() method on string
# which will perform 2 splits on the basis of separator lower case 'd'
a_list = s1.split('d', 2)
# Priting all the splits
print('2 Splits when the separator is d :', a_list)
String is : Eddie edited it
2 Splits when the separator is e : ['Eddi', ' ', 'dited it']
3 Splits when the separator is i : ['Edd', 'e ed', 'ted ', 't']
1 split when the separator is t : ['Eddie edi', 'ed it']
2 Splits when the separator is d : ['E', '', 'ie edited it']
Advertisement
Advertisement
Please check our latest addition
C#, PYTHON and DJANGO
Advertisement