Slicing issues in Python

Source: Internet
Author: User

For a few characters in a string, different languages have different solutions, and Python has a way of slicing.
In the list, the following:


S=list (range (1,101))
If you want to pick an even number of numbers (or select even), you can use the Loop method: But the method is bloated, compared to "stupid"
But the slicing method given in Python is more elegant, as follows:

    L=list (range (1,101))    print (l[0])    print (l[0:10]) #输出结果是 [1, 2, 3, 4, 5, 6, 7, 8, 9, +]    print (L[1:3]) #输出的结果是 [ 2,3]

When the parameter is only two, the parameter preceding the colon is the index value of the start character, and the argument after the colon is the index value of the ending character. Note that the output here is a character that does not contain the following index value.
When there are three parameters, the last parameter x indicates that one character is selected every x.
Example:
Print (L[4:50:5])
The output is a multiple of five.
Of course, the parameter can also be negative, indicating the number of countdown
Also, it can be omitted when the parameter is 0.

Slicing issues in Python

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.