#isalpha()判斷字串是否只為字母和漢字
test = '
alex國
(test.isalpha())
#isdecimal()判斷字串是否為數字
#isdigit()判斷字串是否為數字,還能判斷其它格式的數字,①-⑨能判斷
#isnumeric()上面兩個都可以判斷,還可以判斷中文數字描述
test1 = '⑨'
test4 = '貳'
(test1.isdecimal(), test1.isdigit(), test1.isnumeric(), test4.isnumeric())
#isidentifier()判斷字串是否是有效的python識別符號,可用來判斷變數名是否合法
test2 = '
def_123
(test2.isidentifier())
#islower()只判斷字串裡的字母是否為小寫
test3 = '
alex國33@
(test3.islower())
#istitle()只判斷字母的首字母是否為大寫
test7 = '
國alex3國@
(test7.istitle())
#isupper()只判斷字母是否全為大寫
test8 = '
alex國8@
(test8.isupper())
#lower()把字母全變為小寫
test9 = '
alex age
(test9.lower())
#title()把字母的首字母變為大寫
test10 = '
alex age
(test10.title())
#upper()把字母全變為大寫
test11 = '
alex age
(test11.upper())
#isprintable()是否存在不可顯示的字元;\t製表符,\n換行符
test5 = '
name\tage\nalex\t18
(test5.isprintable())
#isspace()判斷是否全部是空格
test6 = '
(test6.isspace())
#*****join()將字串中的每乙個元素按照指定分隔符進行拼接
msg = '
abc'
msg1 = '
'.join(msg)
(msg1)
#ljust()字串文字靠左,指定長度及填充內容,內容預設空格
msg2 = '
alex
'print(msg2.ljust(10, '*'
))#rjust()字串文字靠右,指定長度及填充內容,內容預設空格
msg3 = '
alex
'print(msg3.rjust(10, '*'
))#zfill()指定內容長度,不能指定填充內容,預設為0
msg4 = '
alex
'print(msg4.zfill(10))
#lstrip()預設清除字串左邊的空白(空格及換行都可以),也可指定去除字元,優先最多匹配
msg5 = '
ab a l e x
'print(msg5.lstrip('
bac'))#
rstrip預設清除字串右邊的空白,也可指定去除字元,優先最多匹配
msg6 = '
a l e x
(msg6.rstrip())
#strip預設清除字串左右邊所有空白及\n換行符,也可指定去除字元,優先最多匹配
msg7 = '
\n a l e x \n
(msg7.strip())
#maketrans(),translate()兩個組合使用,替換的功能
message = '
abcfghhgfcbatyuhgfbdsa
'x = str.maketrans('
abc', '
123'
(message.translate(x))
#partition()以特定字元分割成3部分,從左到右遇到第乙個即停止,特定字元為一部分,左右各一部分;如果沒有遇到指定分割字元,那麼右邊兩部分為空
#rpartition()同上,如果沒有遇到指定分割字元,那麼左邊兩部分為空
message1 = '
asddsafghhgf
'print(message1.partition('
d'), message1.rpartition('d'
))#split()以特定字元分割,但分割後的字元取不到,第二個引數代表分割幾次;
#rsplit()同上,從右邊開始分割;
message2 = '
asddsafghhgf
'print(message2.split('
d', 2), message2.rsplit('
f', 3))
#splitlines()只能根據換行符進行分割,引數true代表輸出包含換行符,引數false代表輸出不包含換行符
message3 = '
asd\nfgh\njkl
(message3.splitlines(true))
#swapcase()大小寫轉換
message4 = '
alex
(message4.swapcase())
#replace()字串替換,最後乙個引數代表替換前幾個
message5 = '
alexa
'print(message5.replace('
a', '
ccc', 1))
'''重點
'''#
join,split,find,strip,lower,upper,replace
#索引,切片,len,for迴圈
#字串一旦建立,不可修改;一旦修改或者拼接,都會造成重新生成字串
#range(0, 100, 2)
字串方法
find 方法可以在乙個較長的字串中查詢子字串,並返回子字串所在位置最左邊的索引。如果沒有找到則返回 1。hello,world.cold enough?find world 6 title hello,world.cold enough?title.find hello 0 title.find ...
字串方法
coding utf 8 字串也可以理解為乙個容器,也存在索引值,而字串中的每乙個字元可以理解為是一 個元素。1 len 獲取字串長度的方法 print 字串長度len len abcd 2 字串的取值 string abcdef r1 string 0 r2 string 1 print r1,r...
字串方法
字串方法 在這裡插入 片 定義乙個字串 var box 我的世界 返回指定位置的字串 box.charat 2 console.log box.charat 2 字串的長度 box.length console.log box.length 英文大小寫 var box2 holle 大寫 box2....