A string slice in Python __python

Source: Internet
Author: User
1. The prototype of the slice operator in Python


[Start:stop:step]

That is: [Start index: End index: Step value]

Start index: As in other languages, starting from 0. Sequence from left to right, the first value is indexed to 0, and the last is-1

End index: The slice operator is taken to the index and does not contain the value of the index.

Step value: The default is one after the other, and if 2, it means a separate fetch operation. The step value is positive to take from left to right and, if negative, to Right-to-left. The step value cannot be 0

Note: If it is string[:] The pattern, then is [Start:stop] 2. Example:


  5 exam= "Abcdefghi"
  6 print Exam[:-1]
  7 print exam[2:]
  8 print exam[:7:2]
  9 print exam[:3:-1]

Output:

Abcdefgh
Cdefghi
Aceg
Ihgfe

Note: the last row, because the third parameter is-1, is output in reverse order, but the index is not in reverse sequence

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.