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'