Extended use of Python slices (slice)

Source: Internet
Author: User
#-*-Coding:utf-8-*-__author__ = ' xy ' #切片 (slice) Extension Usage # # # # of slices with a step index to expand # Slice[1:2:3] where the 3rd position represents the step size, the default is # Simple str flip: s = ' ABCDE ' Print s[::-1]# >>> ' EDCBA ' #间隔一个字符取值: Print s[::2]# >>> ' ace ' #2 Slice index does not cross print (' A ', ' B ', ' C ') [- 100:100]# >>> (' A ', ' B ', ' C ') #3 [None] enhancements to the FOR loop: #每次循环把最后一个字符干掉: s = ' abcde ' i = -1for i in range ( -1, Len (s),-1): 
  
   print s[:i]# >>># abcd# abc# ab# a# If you want to complete output ' ABCDE ' for the first time, you generally need to add a print statement to the for front # see the following improvements: s = ' abcde ' for i in [none]+ Range ( -1,-len (s),-1):    print s[:i]# >>># abcde# abcd# abc# ab# A
  

Extended use of Python slices (slice)

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.