amigo幸运字符什么意思_转载 | 史上最全 python 字符串操作指南
點擊藍字關(guān)注,創(chuàng)智助你長姿勢
【本文已由 ?清風Python?授權(quán)轉(zhuǎn)載(原創(chuàng))作者:王翔,轉(zhuǎn)載請聯(lián)系出處】
字符串的定義
完了,估計很多人看到這個標題就要關(guān)網(wǎng)頁了,稍等不妨再往下看看?
python 定義字符、字符串沒有 java 那樣的嚴格,不管是單引號、雙引號、甚至是三個單引號和雙引號都可以用來定義字符(串),只要成對出現(xiàn)即可。比如:
# 單個字符a='a'# 使用單引號定義字符串name='Uranus'# 使用雙引號定義字符串code = "Hello World ..."# 既然說到了string,怎么能不點開源碼看看呢?class str(object): """ str(object='') -> str str(bytes_or_buffer[, encoding[, errors]]) -> str Create a new string object from the given object. If encoding or errors is specified, then the object must expose a data buffer that will be decoded using the given encoding and error handler. Otherwise, returns the result of object.__str__() (if defined) or repr(object). encoding defaults to sys.getdefaultencoding(). errors defaults to 'strict'. """雖然這些不是主要說的,但還是簡單提下,三個單引號或者雙引號,主要是用來作為文檔注釋的,請不要拿來定義字符串(雖然這樣并不會出現(xiàn)語法錯誤)。
今天主要說下關(guān)于打段的字符串應(yīng)該如何定義,PEP8 有規(guī)定,一行代碼的長度請勿超過 120 個字符。那么如果遇到這種情況,該怎么辦?
# 不推薦的使用方式:line = """Create a new string object from the given object.If encoding or errors is specified,then the object must expose a data buffer that will bedecoded using the given encoding and error handler."""# 或者這樣line = "Create a new string object from the given object. " \ "If encoding or errors is specified," \ "then the object must expose a data buffer that will be" \ " decoded using the given encoding and error handler."# 更好的實現(xiàn)方式:line = ("Create a new string object from the given object." "If encoding or errors is specified," "then the object must expose a data buffer that will be " "decoded using the given encoding and error handler." )字符串中簡單的 .is() 與 .() 的用法
.is()*, 既然是 is,那么它返回的結(jié)果只有兩種,True or False 。先來對比一下數(shù)字:
isdigit()
True: Unicode 數(shù)字,byte 數(shù)字(單字節(jié)),全角數(shù)字(雙字節(jié)),羅馬數(shù)字
False: 漢字數(shù)字
Error: 無
isdecimal()
True: Unicode 數(shù)字,全角數(shù)字(雙字節(jié))
False: 羅馬數(shù)字,漢字數(shù)字
Error: byte 數(shù)字(單字節(jié))
isnumeric()
True: Unicode 數(shù)字,全角數(shù)字(雙字節(jié)),羅馬數(shù)字,漢字數(shù)字
False: 無
Error: byte 數(shù)字(單字節(jié))
總結(jié)幾個偏門知識點:
a='①②③④⑤'isdigit()、isnumeric() 為 True isdecimal() 為 Falseb='一壹'isnumeric() 會認為是 True 的哦!再來看一個等式:
isalnum() = isdigit() + isalpha() + isspace()
isdigit() 表示字符串內(nèi)全部為數(shù)字
isalpha() 表示字符串內(nèi)全部為字符
isspace() 表示字符串有一個或多個空格組成
isalnum() 表示字符串內(nèi)全部為數(shù)字和字符
針對字符串大小寫的方法:
.isupper() 字符串全部由大寫組成
.islower() 字符串全部由小寫組成
.istitle() 字符串形式為駝峰命名,eg:"Hello World"
以上這些用法去掉 is ,則變?yōu)榱藢?yīng)的字符串轉(zhuǎn)發(fā)方法。學一套會兩套,買一送一....
最后說一個不帶 . 的 is* --- isinstance(obj,type)
判斷一個 object 是什么類型...
type 可選類型為:int,float,bool,complex,str,bytes,unicode,list,dict,set,tuple
并且 type 可以為一個原組:isinstance(obj, (str, int))
判斷字符串中的內(nèi)容
.*with() starts ends 不僅支持開頭結(jié)尾的匹配,還支持 start 和 end 兩個參數(shù)來動態(tài)定義字符串的 index 位置
long_string = "To live is to learn,to learn is to better live"long_string.startswith('To')long_string.startswith('li', 3, 5)long_string.endswith('live')long_string.endswith('live', 0, 7)同樣支持 start、end 來判斷字符串的還有 .find()、.rfind() 和? .index()、.rindex()
這兩類字符串尋址方法均支持從左到右、從右至左兩種尋址方式,不同的是:
find 在未找到時,返回 -1,而 index 在未找到時,會拋出 ValueError 的異常...
long_string.index('live') # 3long_string.rindex('live') # 42字符串的內(nèi)容變更
狹義來說使用,字符串的替換使用 .replace() 即可,那為什么還要單獨說呢?因為它有一個可選你參數(shù) count
long_string = "To live is to learn,to learn is to better live"long_string.count('live') # 2long_string.replace('live','Live',1)output:'To Live is to learn,to learn is to better live'# 可以看到,第二個live并未進行替換剛才說了狹義,那么廣義呢?
(l/r)strip()
將字符串左、右、兩端的特定字符過濾掉,默認為空格...
strip() 要注意的地方是,strip('TolLive') 中的字符并非完整匹配,而是針對每一個字符進行匹配,說起來混,直接上例子:
字符串切片
字符串的切片分為 long_string[start:end;step] start、end 區(qū)間為左閉右開...這個網(wǎng)上說的太多了,再拉出來詳細講就要挨打了...
(l/r)just(width,[fillchar])、center(width, [fillchar])、zfill(width)
這些均為填充固定長度的字符,默認使用空格 ( zfill 為左補 0,z 是 zero 的意思...),看意思就明白了,不用多講了....
字符串格式化輸出
本來 fill 和 center 等可以放在這里,但是他們使用頻率和重量級不夠格,就丟在上面了。
Python 格式化輸出分為兩類,那是在 pyton2 的時代,即 %? 和 format 。這兩種網(wǎng)上的資料太多了,說的太多顯得沒逼格...
但,還是要簡單說說其中特殊的地方
% 格式化輸出:
如何在 % 的格式輸出中,輸出用來看做標記為的 % 符號呢?使用兩個百分號(%%)
%(-)(width) width 為設(shè)置長度,默認左填充空格,添加 - 號為右填充
width 代表字符串截斷,保留多少長度的字符串
type %s 字符串 %d 十進制整數(shù) %f 小數(shù) ...
多個參數(shù)是,后面的參數(shù)需要使用括號包裹起來
format 格式輸出:
format 在 python3 開始官方就表示為替換 % 的輸出方式,之所以還保留著 %,主要是為了兼容性考慮...
對比 %,format 使用花括號 {} 表示變量
< > ^? 代表了 format 的對齊方式
f-string
Python3.6 的版本更新時,新增了 f-string,英文好的可以去看官方解釋 PEP 498 -- Literal String Interpolation 。
f-string 是字符串引號前以 f/F 開頭,并使用 {} 標注替換位置的使用形式。
之所以官方推出 f-string,主要是因為它的更高的性能、更強的功能。例子走起:
name = 'Uranus'f'Hello,{name}'f'Hello,{name.lower()}'f'Hello,{name:^10s}'f'Hello,{(lambda x: x*2) (name)}'output:'Hello,Uranus''Hello,uranus''Hello, Uranus ''Hello,UranusUranus'上期內(nèi)容:轉(zhuǎn)載 | 揭秘:嵌入式中究竟該如何活用“延遲”?
下期內(nèi)容:轉(zhuǎn)載 | 寫幾個 Python 進階必備函數(shù)
?創(chuàng)智俱樂部
微信:sziitlSA
一個讓你漲姿勢的社團
長按二維碼關(guān)注
總結(jié)
以上是生活随笔為你收集整理的amigo幸运字符什么意思_转载 | 史上最全 python 字符串操作指南的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 为什么多个线程不可能同时抢到一把锁_并发
- 下一篇: mysql关于日期的函数_MySQL中关