Python slice operation instance analysis,

Source: Internet
Author: User

Python slice operation instance analysis,

This example describes the Python slicing operation. We will share this with you for your reference. The details are as follows:

In many programming languages, a truncation function is provided for strings, which is used to segment strings. Python does not have a string truncation function. You only need to perform the slicing operation.

The Slice operator is a sequence name followed by a square bracket. There are three optional numbers in the square brackets and are separated by a colon. The number is optional, and the colon is required.

The first number in the slice operator indicates the start position of the slice, the second number indicates where the slice ends, and the third number indicates the slice step size.

If the first number is not specified, Python starts from the beginning of the sequence. If the second number is not specified, Python stops at the end of the sequence. If no third number is specified, the step size defaults to 1.

Note:The returned sequence starts from the starting position and ends just before the ending position. That is, the start position is included in the sequence slice, and the end position is excluded from the slice, forming a pre-closed and post-open interval [)

For example:

1 forward slice (step size is a positive integer)

>>> S = 'Love python! '>>> S [] # slice interval [), step 2 'v yh' >>> s [: 5] # slice interval ), the default step size is 1 'love'> s [4:] # slice interval [4, len (s). The default step size is 1 'python! '>>> S [:] # slice interval [0, len (s), step 1, return the copy of the entire sequence 'Love python! '>>> S [: 2] # The slice interval is [0, len (s), and the step size is 2 'lv yhn'

The index of the last element in the Python sequence is-1. python also supports negative index slicing. For example:

>>> S' love python! '>>> S [-5:-2] # slice interval [-5,-2). The default step size is 1 'tho' >>> s [: -1] # except for the last element, all other elements return 'Love python' >>> s [-8: 2] # slice interval [-8,-1], the step size is 2 'yhn'

2 reverse slice (step size is negative)Such:

>>> S' love python! '>>> S [9: 2:-1] 'ohtyp E' >>>> s [-1:-1] # returns the reverse sequence of the sequence '! Nohtyp evol '>>> s [:-1] # returns the reverse sequence of the sequence '! Nohtyp evol'

In addition, slice can act on strings, tuples, and lists.

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.