This is Rem, a liberal arts programmer. Recently, as a personal study, I've been knocking 100 language processes, but I'm impressed with how convenient the slices are.
I don't operate such character strings in Java that I usually use (maybe I just don't know because of lack of study, please tell me who knows).
Slice notation selects a range of elements in a sequence object (string, tuple or list). Slice notation can be used as an expression, as an assignment or as a del statement. Quote: http://docs.python.jp/2.7/reference/expressions.html?highlight=%E3%82%B9%E3%83%A9%E3%82%A4%E3%82%B9
I see. Everyone wants that the primary evaluation value result must be a sequence type object.
I wonder if it's a little easier to understand. .. ..
The slice can specify any part of the character string by specifying the start position, end position, and step width.
sequence [Start position: End position: Step width]
Also, it seems that these can be omitted. Let's try it with a simple example!
Sample.py
str = u"It's sunny today!"
print (str[::2])
Result: Heaven now!
This time, the step width is specified by 2, so it looks like this. When the step width is 2, the lie is taken out every two elements such as the 0th element, the 2nd element, and so on. By the way, since the start position is 0, beginners cannot retrieve the desired element unless they are careful.
This time, the start position and end position are specified.
Sample.py
str = u"It's sunny today!"
print (str[1:3:])
Result: sunny day
In this way, it specifies the character string from the start to the end.
By the way, if you specify the end position, start position, and step width as something that does not exist in the character string, nothing will be extracted.
Slicing is profound. However, it may reduce readability, so it may not be good to specify it with a negative value.
Recommended Posts