This article is an example of the Python data type "colon" [:::] - slice and step operations. Shared for your reference, as follows:
For example, there is the following string:
string = "welcome to jb51^_^"
You can use the slice and step characters: to slice a string and define a step.
string = "welcome to jb51^_^" # Returns all by default print string[:] # Returns results from 1 to 9 print string[1:9] # Returns results from 1 to 9 in steps of 1. print string[1:9:] # Returns results from 1 to 9 in steps of 2. print string[1:9:2] # Returns results from 1 to 9 in steps of -1. print string[1:9:-1] # Transpose print string[::-1]
The results are as follows.
Here it is.
# Returns results from 1 to 9 in steps of -1. print string[1:9:-1]
does not output the reverse order of 1 through 9, which would then bestring[1:9]
Just look at it as the first string and transpose it.
# Returns results from 1 to 9 in steps of -1. print string[1:9][::-1]
Determining whether a substring of a string is a palindrome would be nifty with this method
Notes:counteracting a phenomenon known asstring[start:end:step]
In the slice of theIf step is positive, start must be less than end; if step is negative, start must be greater than end.。
Readers interested in more Python related content can check out this site's topic: thePython list (list) manipulation techniques summarized》、《Summary of Python array manipulation techniques》、《Summary of Python string manipulation techniques》、《Summary of Python function usage tips》、《Python introductory and advanced classic tutorialsand thePython Data Structures and Algorithms Tutorial》
I hope that what I have said in this article will help you in Python programming.