Python Learning: String slicing operations

Source: Internet
Author: User

One: Takes the first character of a string

print "Hello" [0] indicates the first character in the output string
print "Hello" [-1] indicates the last character in the output string


Two: String segmentation

print "Hello" [1:3]

#第一个参数表示原来字符串中的下表
#第二个阐述表示分割后剩下的字符串的第一个字符 subscript in the original string

This sentence is a bit verbose, directly look at the output:

El


Three: Several special cases

(1) print "Hello" [: 3] Start with the first character intercept

(2) print "Hello" [0:] intercept from the first character and intercept to the last

(3) print "Hello" [:] the same (2)

Look at the results of the output:

Hel
Hello
Hello


Four: Step intercept

print "Hello" [:: 2]
print "Hello" [::-2]

Represents the intercept from the first character and takes one of the 2 characters in each interval.


Directly look at the output results:


Hlo
Olh



Python Learning: String slicing operations

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.