The split function separates and lists character strings that are a mixture of numbers, alphabets, symbols, etc., according to a certain rule. Writing a program that separates strings from scratch can be very cumbersome, and for beginners it can be a daunting task that can take a day if you're not good at writing it. The Split function is a very convenient function that frees you from such complexity. Just knowing this will change your work efficiency considerably.
table of contents 1 [Basic usage of split function](## Basic usage of split function) 2 [Application of delimiter](## Application of delimiter) 3 [rsplit function](## rsplit function)
For example, consider the case where the data you created is separated by "," as shown below.
>>> print test
orange,apple,banana,strawberry
You can get a list separated by a delimiter by using the string name .split (“delimiter”).
>>> test.split(",") #The list called test is delimited by ",Separated by
['orange', 'apple', 'banana', 'strawberry']
Also, when the delimiter is a space, line feed, or tab, you can separate it without specifying a delimiter.
>>> test = "orange apple banana strawberry"
>>> test.split() #If nothing is specified in the argument, it will be automatically separated by spaces or tabs.
['orange', 'apple', 'banana', 'strawberry']
If you use this, for example, even if there are two spaces in the delimiter, you can divide it neatly.
>>> test = "2016 November 15" #It is separated by two spaces.
>>> test.split() #
['2016', 'November', '15']
Even if there are multiple delimiters between the characters you want to divide in this way, you can easily handle it with this.
Furthermore, if you want to do something crowded, for example, "Separate up to the second, do not separate the third and subsequent ones", you can do as follows.
>>> test = "orange,apple,banana,strawberry"
>>> test.split(",", 2) #Divide up to the second time, and do not divide after the third time.
['orange', 'apple', 'banana,strawberry']
Then, what if you want to separate only the first one without specifying the delimiter? For example, when I tried the following, I got an error.
>>> test = "2016 November 15" #It is separated by two spaces.
>>> test.split(1) #I wanted to separate only the first one. .. ..
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: expected a character buffer object
In Python terms, the first argument of the split function seems to be useless without a delimiter.
In this case, None is used to mean "the user does not explicitly specify the delimiter".
>>> test = "2016 November 15" #It is separated by two spaces.
>>> test.split(None,1)
['2016', 'November 15']
By doing this, I was able to successfully divide only the first one.
We will also introduce the rsplit function, which has a sister relationship with the split function. rsplit is basically the same as split. However, split has a delimiter order from the beginning of the string, while rsplit has a delimiter order from the end of the string. As an aside, the "r" in rsplit seems to come from right, which means from the right side of the string, or rear, which means after.
Let's actually go.
>>> test = "orange,apple,banana,strawberry"
>>> test.rsplit(",") # “,Separate with ”. There is no limit to the number of divisions.
['orange', 'apple', 'banana', 'strawberry']
If only the delimiter is given to the argument of rsplit in this way, the rsplit function = split function. Now, add the number of delimiters as an argument (= 2).
>>> test = "orange,apple,banana,strawberry"
>>> test.rsplit(",",2)
['orange,apple', 'banana', 'strawberry']
You can see that the second from the back is certainly separated.
Furthermore, the delimiter does not have to be one character. Strings are perfectly fine and the split and rsplit functions work.
>>> test = "orangehogeapplehogebananahogestrawberry"
>>> test.split("hoge") #The string hoge corresponds to the delimiter
['orange', 'apple', 'banana', 'strawberry']
Recommended Posts