String [] Python basis of 11_Python

1. Definition of the string

You may be used "" double quotes may be used '' single quotes define a string, defined generally in double quotes.

2. The operation of the string

Determine the type:

 Find and Replace

Case conversion:

Text alignment

NOTE: string.center (weight, str ) to align the filling str, similar to the other two methods, can be expanded.

Remove whitespace characters

Split and links

 

3. Slice the string

definition

String [Start Index: Index End: step]  includes a front does not include back

 1 str1 = "Hello,World"
 2 # 截取索引2-5的字符
 3 print(str1[2:6])  # llo,
 4 # 从索引2开始,截取到最后
 5 print(str1[2:])  # llo,World
 6 # 从0开始截取到索引5
 7 print(str1[:6])  # Hello,
 8 # 从0开始截取到最后
 9 print(str1[:])  # Hello,World
10 # 从零开始,步长为2,截取到最后(每两个截取一个)
11 print(str1[::2])  # HloWrd
12 # 从索引1开始,步长为2,截取到最后(从索引1开始,每隔一个取一个)
13 print(str1[1::2])  # el,ol
14 # 截取索引为-1的,截取最后一个字符
15 print(str1[-1])  # d
16 # 从索引2开始截取到倒数第二个(不包括倒数第一个)
17 print(str1[2:-1])  # llo,Worl
18 # 从倒数第二个开始,截取到最后
19 print(str1[-2:])  # ld
20 
21 # 从最后一个开始,步长为-1(使用字符串切片,让字符串逆序)
22 print(str1[-1::-1])

Guess you like

Origin www.cnblogs.com/dujinyang/p/11261472.html