上一章介紹了python中的關鍵字、變數、輸入輸出、注釋、還有資料型別等概念,接下來這篇文章主要介紹python中字串的相關筆記,文章只按照我自己覺得重點的知識點去列舉,不會列舉特別細致的點,
字串定義:成對的單引號或者是成對的雙引號、三引號括起來的字符內容,
字串索引:字串里面的每個字母都是有索引的,索引也就是每個字符對應的位置,那么索引的順序有2種:
正序:從左到右索引默認0開始的,最大范圍是字串長度少1
反序:從右到左索引默認-1開始的,最大范圍是字串開頭
字串轉義

可以使用反斜杠來轉義,比如\n表示換行,\t表示制表符,使用r可以讓反斜杠不發生轉義,
a = "hello\tworld"b = r"hello\tworld"分別列印a和b的值:hello worldhello\tworld
字串的連接和重復
字串可以使用+進行拼接
可以使用*號列印重復字串
a = 'ac'b = 'ad'print(a+b)print(a*3)
字串的值不能被改變,不能直接更改字串的某一字符的值
比如,定義了一個字串,name='小博',嘗試去改字串的第一個字符的時候,name[0]='李' ,執行是會報TypeError: 'str' object does not support item assignment錯的,說明,字串的值是不允許進行更改的,
那么,為什么對字串變數重新賦值又不會報錯呢?使用name=‘'xxx' 實際上是指向了一個新的字串,
可以使用id()函式驗證一下效果:
name = 'xiaobo'print(id(name))name = 'libo'print(id(name))列印出來的id是不一樣的
雖然不能直接修改,但是可以間接修改字串的值,生成一個新的字串:???????
name = '小博'print(id(name))name1 = name.replace('小','李')print(id(name1))列印出來的id值是完全不一樣的
字串截取(切片)
一個完整的切片運算式:str[start_index:end_index:step]
step:正負數均可,其絕對值大小決定了切取資料時的‘‘步長”,而正負號決定了“切取方向”,正表示“從左往右”取值,負表示“從右往左”取值,當step省略時,默認為1,即從左往右以步長1取值,
start_index:表示起始索引(包含該索引對應值);該引數省略時,表示從物件“端點”開始取值,至于是從“起點”還是從“終點”開始,則由step引數的正負決定,step為正從“起點”開始,為負從“終點”開始,
end_index:表示終止索引(不包含該索引對應值,即類似于數學里面的開區間);該引數省略時,表示一直取到資料“端點”,至于是到“起點”還是到“終點”,同樣由step引數的正負決定,step為正時直到“終點”,為負時直到“起點”,???????
name = 'hello world'print(name[0:5:1], name[:5:]) #hello helloprint(name[::]) #hello worldprint(name[::-1]) #dlrow olleh
判斷字串是否滿足某種格式要求???????
print('123'.isdigit())#為純數字回傳True,否則回傳Falseprint('NAME'.isupper())#為純大寫字母回傳True,否則回傳Falseprint('name'.islower())#為純小寫字母回傳True,否則回傳Falseprint(' '.isspace())#為純空格時回傳True,否則回傳Falseprint('sdfjklsdjkflsdjl'.isalpha())#為純字母時回傳True,否則回傳Falseprint('sadsfj23423423'.isalnum())#為字母或數字時回傳Ture,否則回傳False以上是python提供的一些現成的方法,其他格式校驗可以采用正則運算式
使用場景:
1、以后做專案的時候,做一些輸入校驗,比如校驗密碼只能是字母或數字,如果符合要求列印True,不符合要求列印False
字串的列印
使用%進行格式化輸出:???????
print("my name is %s" % ("xiaobo"))print("my name is %.2s" % ("xiaobo")) # 字串截取 %.2s 保留2個長度的字串print("my age is %d" % (29.2)) # 輸出整數 %dprint("my salaly is %f" % (29)) # 輸出浮點數 %fprint("my height is %f m" % (1.73))print("my height is %.2f m" % (1.73)) # 指定輸出小數點位數 %.3f (保留到小數點后3位)輸出的結果分別對應如下:my name is xiaobomy name is ximy age is 29my salaly is 29.000000my height is 1.730000 mmy height is 1.73 m
使用format函式進行格式化輸出:
相對基本格式化輸出采用‘%’的方法,format()功能更強大,該函式把字串當成一個模板,通過傳入的引數進行格式化,并且使用大括號‘{}’作為特殊字符代替‘%’,
使用方法:
-
不指定序號,自動去匹配{}{}
-
指定序號去匹配{0}{1}
-
指定同一個序號去匹配{1}{1}
-
指定序號+格式化輸出去匹配{0:d}{1:.2f},要注意序號與冒號之間不能有空格
-
指定key的方式去匹配{name}{age}
-
指定key+格式化輸出去匹配{0:d}{1:.2f},要注意key與冒號之間不能有空格
使用案例:???????
print("my name is {},age is {}".format("xiaobo", 29))print("my name is {0},age is {0}".format("xiaobo", 29))print("my name is {0},age is {1}".format("xiaobo", 29))print("my name is {0},age is {1},height is {2:.2f}".format("xiaobo", 29, 173))print("my name is {name},age is {age},height is {height:.2f}".format(height=173, age=29, name="xiaobo"))print("my name is {name:s},age is {age:d},height is {height:.3f}".format(height=173, age=29, name="xiaobo"))分別對應輸出如下:my name is xiaobo,age is 29my name is xiaobo,age is xiaobomy name is xiaobo,age is 29my name is xiaobo,age is 29,height is 173.00my name is xiaobo,age is 29,height is 173.00my name is xiaobo,age is 29,height is 173.000
字串的格式化控制符及其說明如下表所示,格式化控制符位于占位符索引或占位符名字的后面,之間用冒號分 隔,語法:{引數序號:格式控制符}或{引數名:格式控制符},
字串的格式化控制符及其說明如下表所示:

轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/300756.html
標籤:python
