下面給大家分享python 字符串string的內置方法,具體內容詳情如下所示:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
|
#__author: "Pizer Wang" #__date: 2018/1/28 a = "Let's go" print (a) print ( "-------------------" ) a = 'Let\'s go' print (a) print ( "-------------------" ) print ( "hello" * 3 ) print ( "helloworld" [ 2 :]) print ( "-------------------" ) print ( "ell" in "helloworld" ) print ( "-------------------" ) print ( "Pizer is a good student" ) print ( "%s is a goog student" % "Pizer" ) print ( "-------------------" ) a = "1234" b = "abcd" c = "!@#$" d = a + b + c print (d) d = "".join([a, b, c]) print (d) d = ", " .join([a, b, c]) print (d) d = "++" .join([a, b, c]) print (d) print ( "-------------------" ) print ( "string的內置方法" ) str = "helloworld" print ( str .count( "l" )) #統計元個數 print ( str .capitalize()) #首字母大寫 print ( str .center( 25 , "-" )) #居中 print ( str .endswith( "d" )) print ( str .endswith( "world" )) print ( str .endswith( "word" )) #是否以某個內容結尾 print ( str .startswith( "hello" )) #是否以某個內容開始 str = "hello\tworld" print ( str .expandtabs(tabsize = 10 )) print ( "-------------------" ) str = "helloworld {name} is {age}" print ( str .find( "w" )) #查找到第一個元素并將索引值返回 print ( str . format (name = "Pizer" , age = 18 )) print ( str .format_map({ "name" : "Jone" , "age" : 25 })) print ( "-------------------" ) print ( str .index( "w" )) #print(str.index("www")) #報錯 print ( str .find( "wwww" )) print ( "-------------------" ) str = "123abc" print ( str .isalnum()) str = "123" print ( str .isalnum()) str = "abc" print ( str .isalnum()) str = "!@$" print ( str .isalnum()) str = "中國萬歲" print ( str .isalnum()) print ( "-------------------" ) print ( "123456" .isdecimal()) print ( "123456ff" .isdecimal()) print ( "123456789" .isdigit()) print ( "12345.6789" .isdigit()) print ( "12345.6789" .isnumeric()) print ( "-------------------" ) print ( "34abc" .isidentifier()) print ( "_34abc" .isidentifier()) print ( "abc" .islower()) print ( "abC" .islower()) print ( "ABC" .isupper()) print ( " " .isspace()) print ( "-------------------" ) print ( "Hello Jone" .istitle()) print ( "Good morning" .istitle()) print ( "-------------------" ) print ( "Hello Jone" .lower()) print ( "Good morning" .upper()) print ( "Hello Jone" .swapcase()) print ( "-------------------" ) print ( "Hello world" .ljust( 20 , "-" )) print ( "Hello world" .rjust( 20 , "-" )) print ( " Hello world \t \n" ) print ( " Hello world " .strip()) print ( " Hello world " .lstrip()) print ( " Hello world " .rstrip()) print ( "-------------------" ) print ( "Hello Jone Jone" .replace( "Jone" , "Pizer" )) print ( "Hello Jone Jone" .replace( "Jone" , "Pizer" , 1 )) print ( "My title" .find( "t" )) print ( "My title" .rfind( "t" )) print ( "-------------------" ) print ( "Hello world" .split( " " )) print ( "Hello world" .split( "l" , 1 )) print ( "Hello world" .rsplit( "l" , 1 )) print ( "hello jone" .title()) print ( "-------------------" ) #重要的字符串方法 # print(st.count('l')) # print(st.center(50,'#')) # 居中 # print(st.startswith('he')) # 判斷是否以某個內容開頭 # print(st.find('t')) # print(st.format(name='alex',age=37)) # 格式化輸出的另一種方式 待定:?:{} # print('My tLtle'.lower()) # print('My tLtle'.upper()) # print('\tMy tLtle\n'.strip()) # print('My title title'.replace('itle','lesson',1)) # print('My title title'.split('i',1)) |
執行結果:
Let's go
Let's go
hellohellohello
lloworld
True
Pizer is a good student
Pizer is a goog student
1234abcd!@#1234abcd!@#
1234abcd!@#
1234, abcd, !@#$
1234++abcd++!@#$
string的內置方法
3
Helloworld
——–helloworld——-
True
True
False
True
hello world
5
helloworld Pizer is 18
helloworld Jone is 25
5
-1
True
True
True
False
True
True
False
True
False
False
False
True
True
False
True
True
True
False
hello jone
GOOD MORNING
hELLO jONE
Hello world———
———Hello world
Hello world
Hello world
Hello world
Hello world
Hello Pizer Pizer
Hello Pizer Jone
3
5
[‘Hello', ‘world']
[‘He', ‘lo world']
[‘Hello wor', ‘d']
Hello Jone
總結
以上所述是小編給大家介紹的python字符串string的內置方法實例詳解,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對服務器之家網站的支持!
原文鏈接:https://blog.csdn.net/Suck_char/article/details/79190200