The "colon" [:] In the Python data type -- slice and step size operation example,

Source: Internet
Author: User

The "colon" [:] In the Python data type -- slice and step size operation example,

The example in this article describes the "colon" [:] In the Python data type -- multipart and step size operations. We will share this with you for your reference. The details are as follows:

For example, the following string is available:

String = "welcome to jb51 ^_^"

You can use the part character and step character to split the string and define the step size.

String = "welcome to jb51 ^_^" # All print string [:] # results from 1 to 9 are returned by default. print string [] # results from 1 to 9 are returned, the step size is 1 print string [1: 9:] # returns the results from 1 to 9. The step size is 2 print string [] # returns the results from 1 to 9, the step size is-1 print string [1: 9:-1] # transpose print string [:-1]

The result is as follows:

Found here

# Return the results from 1 to 9. The step size is-1 print string [1: 9:-1]

No reverse order from 1 to 9 is output.String [1: 9]As the first string, and then transposed.

# Return the results from 1 to 9. The step size is-1 print string [] [:-1].

It is very spiritual to use this method to determine whether the substring of a string is a text string.

Note:Target ImageString [start: end: step]When the step is positive, start must be smaller than end. If the step is negative, start must be greater than end.

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.