Python study note 7: String operations (1)

Source: Internet
Author: User

 

 1 # Coding: UTF-8
2 # string operations
3 # Use brackets [] to extract any consecutive character from the string
4 # Note: The expression in the brackets is a string index, which indicates the position of the character in the string,
5 # the index of the first character of the string in the brackets is 0, not 1
6 # Len returns the length of the string
7
8. test_string = "1234567890"
9 print test_string [0] # result = 1
10 print test_string [1] # result = 2
11 print test_string [9] # result = 0
12 print Len (test_string) # result = 10
13
14 # Use for loop to traverse strings
15 For I in test_string:
16 print I
17 if (I = '5 '):
18 print "Aha, I find it! "
19 print Type (I) # <type 'str'>
20
21 # extract part of a string
22 # operator [N: m] returns part of the string. Starts from the nth string and ends with the nth string.
23 # contains N, but not M.
24 # If n is ignored, the returned string starts from index 0.
25 # If m is ignored, the string starts from n and ends with the last string.
26
27 test_string = 'abcdefghijklmnopqrstuvwxy'
28 print test_string [0: 5] # result = 'abcde'
29 print test_string [8: 11] # result = 'ijk'
30 print test_string [: 6] # result = 'abcdef'
31 print test_string [20:] # result = 'uvwxyz'
32 print test_string [:] # result = 'abcdefghijklmnopqrstuvwxy'

 

 

 

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.