1.將字串的第乙個字母轉換為大寫,其餘字母轉換成小寫。
函式形式:capitalize()
輸出:str =
"hello world!"
("str.capitalize():"
, str.
capitalize()
)
注意:str.
capitalize()
: hello world!
[finished in
0.2s]
如果字串中首字元非字母,那麼首字母不會轉換成大寫,會轉換成小寫。
輸出:str =
"666 hello world!"
("str.capitalize():"
, str.
capitalize()
)
2.判斷字串中字母是否為大寫。str.
capitalize()
:666 hello world!
[finished in
0.2s]
函式形式:isupper()
輸出:str1 =
"hello world!"
str2 =
"hello world!"
(str1.
isupper()
(str2.
isupper()
)count =
0for i in str1:
if i.
isupper()
: count +=
1print
(count)
3.判斷字串中字母是否為小寫。true
false
10[finished in
0.2s]
函式形式:islower()
輸出:str1 =
"hello world!"
str2 =
"hello world!"
(str1.
islower()
(str2.
islower()
)count =
0for i in str2:
if i.
islower()
: count +=
1print
(count)
4.字母大小寫轉換。false
true
10[finished in
0.2s]
函式形式:upper()
函式形式:lower()
函式形式:swapcase()
輸出:str1 =
"hello world!"
str2 =
"hello world!"
str3 =
"hello world!"
(str1.
upper()
(str2.
lower()
(str3.
swapcase()
)
5.字串中字母判斷。hello
world
!hello world!
hello world
![finished in
0.2s]
函式形式:isalpha()
輸出:str =
"1a+"
for i in str:
(i.isalpha()
)
6.輸出字串中最大、最小字母false
true
false
[finished in
0.1s]
函式形式:max()
函式形式:min()
輸出:str =
"helloworld"
(max
(str)
(min
(str)
)
1.統計字串裡某個字元出現的次數。可選引數為在字串搜尋的開始與結束位置。r
h[finished in
0.2s]
函式形式:count()
str.count(sub, start= 0,end=len(str))
輸出:str =
"hello world"
(str.
count
('o'))
(str.
count
('o',0
,5))
2.檢測字串中是否包含指定字元(串),返回第乙個找到的字元的索引值2
1[finished in
0.1s]
函式形式:index()
輸出:str =
"hello world"
(str.
index
('o'))
(str.
index
('or'
))
1.檢測字串是否由字母和數字組成。4
7[finished in
0.2s]
函式形式:isalnum()
輸出:str1 =
"helloworld666"
str2 =
"helloworld666!"
(str1.
isalnum()
(str2.
isalnum()
)
2.檢測字串是否只由數字組成。true
false
[finished in
0.2s]
函式形式:isdigit()
函式形式:isnumeric()
輸出:str1 =
"666"
str2 =
"\u00bd" #str2 =
'1/2'
(str1.
isnumeric()
(str1.
isdigit()
(str2.
isnumeric()
(str2.
isdigit()
)
3.返回指定長度的字串,原字串右對齊,前面填充0。true
true
true
false
[finished in
0.2s]
函式形式:zfill()
輸出:a =
bin(
int(2)
)[2:
(a.zfill(8
(a.zfill(32
))
更多的python的字串內建函式,請檢視菜鳥教程》python3字串》python的字串內建函式(00000010
00000000000000000000000000000010
[finished in
0.2s]
sum 轉字串 Python字串與內建函式
字串 建立變數來儲存字串 字串可以通過單 雙 三引號建立字串 message hello,world 變數mseeage,值為 hello,world print message 輸出結果 hello,world python3,有3種數值型別分別為 int 整形 建立變數為a,值為496 a 49...
Python的partition字串函式
rpartition s.rpartition sep head,sep,tail search for the separator sep in s,starting at the end of s,and return the part before it,the separator itsel...
python 內函式 Python 常見內建函式
map map 會根據提供的函式對指定序列做對映。第乙個引數 function 以引數序列中的每乙個元素呼叫 function 函式,返回包含每次 function 函式返回值的新列表。在python2中返回列表,在python3中返回迭代器。def square x return x 2 prin...