Advertisement



< Prev
Next >



Python - String split() Method





As per the name suggests, the split() string method used to split the content of the invoked string by starting at the left of the string and at the occurrence of a specificseparator. The split() method returns a list of split values.

Note : By default, the slit() method splits the content of a string at the occurrence of each whitespace in it but you can specify your own separator as well.




Signature of split() method


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 whitespace.

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.







When calling the rsplit() method, we can even specify a maximum number(and not more than that) of splits to perform on the invoked string(starting from the left of string), on the basis of a separator. Let us see an example.

# 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)


Output is :


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']





Please share this article -





< Prev
Next >
< String count() Method
String rsplit() Method >



Advertisement

Please Subscribe

Please subscribe to our social media channels for daily updates.


Decodejava Facebook Page  DecodeJava Twitter Page Decodejava Google+ Page




Advertisement

Notifications



Please check our latest addition

PYTHON and DJANGO


Advertisement