日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程语言 > python >内容正文

python

amigo幸运字符什么意思_转载 | 史上最全 python 字符串操作指南

發(fā)布時(shí)間:2024/7/19 python 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 amigo幸运字符什么意思_转载 | 史上最全 python 字符串操作指南 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

點(diǎn)擊藍(lán)字關(guān)注,創(chuàng)智助你長(zhǎng)姿勢(shì)

【本文已由 ?清風(fēng)Python?授權(quán)轉(zhuǎn)載(原創(chuàng))作者:王翔,轉(zhuǎn)載請(qǐng)聯(lián)系出處】

字符串的定義

完了,估計(jì)很多人看到這個(gè)標(biāo)題就要關(guān)網(wǎng)頁(yè)了,稍等不妨再往下看看?

python 定義字符、字符串沒(méi)有 java 那樣的嚴(yán)格,不管是單引號(hào)、雙引號(hào)、甚至是三個(gè)單引號(hào)和雙引號(hào)都可以用來(lái)定義字符(串),只要成對(duì)出現(xiàn)即可。比如:

# 單個(gè)字符a='a'# 使用單引號(hào)定義字符串name='Uranus'# 使用雙引號(hào)定義字符串code = "Hello World ..."# 既然說(shuō)到了string,怎么能不點(diǎn)開(kāi)源碼看看呢?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'. """

雖然這些不是主要說(shuō)的,但還是簡(jiǎn)單提下,三個(gè)單引號(hào)或者雙引號(hào),主要是用來(lái)作為文檔注釋的,請(qǐng)不要拿來(lái)定義字符串(雖然這樣并不會(huì)出現(xiàn)語(yǔ)法錯(cuò)誤)。

今天主要說(shuō)下關(guān)于打段的字符串應(yīng)該如何定義,PEP8 有規(guī)定,一行代碼的長(zhǎng)度請(qǐng)勿超過(guò) 120 個(gè)字符。那么如果遇到這種情況,該怎么辦?

# 不推薦的使用方式: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."# 更好的實(shí)現(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." )

字符串中簡(jiǎn)單的 .is() 與 .() 的用法

.is()*, 既然是 is,那么它返回的結(jié)果只有兩種,True or False 。先來(lái)對(duì)比一下數(shù)字:

isdigit()
True: Unicode 數(shù)字,byte 數(shù)字(單字節(jié)),全角數(shù)字(雙字節(jié)),羅馬數(shù)字
False: 漢字?jǐn)?shù)字
Error: 無(wú)

isdecimal()
True: Unicode 數(shù)字,全角數(shù)字(雙字節(jié))
False: 羅馬數(shù)字,漢字?jǐn)?shù)字
Error: byte 數(shù)字(單字節(jié))

isnumeric()
True: Unicode 數(shù)字,全角數(shù)字(雙字節(jié)),羅馬數(shù)字,漢字?jǐn)?shù)字
False: 無(wú)
Error: byte 數(shù)字(單字節(jié))

總結(jié)幾個(gè)偏門(mén)知識(shí)點(diǎn):

a='①②③④⑤'isdigit()、isnumeric() 為 True isdecimal() 為 Falseb='一壹'isnumeric() 會(huì)認(rèn)為是 True 的哦!

再來(lái)看一個(gè)等式:

isalnum() = isdigit() + isalpha() + isspace()
isdigit() 表示字符串內(nèi)全部為數(shù)字
isalpha() 表示字符串內(nèi)全部為字符
isspace() 表示字符串有一個(gè)或多個(gè)空格組成
isalnum() 表示字符串內(nèi)全部為數(shù)字和字符

a='12345'b='①②③④⑤'c='abc123'print(a.isdigit()) # Trueprint(b.isalpha()) # Trueprint(c.isalnum()) # True

針對(duì)字符串大小寫(xiě)的方法:

.isupper() 字符串全部由大寫(xiě)組成
.islower() 字符串全部由小寫(xiě)組成
.istitle() 字符串形式為駝峰命名,eg:"Hello World"

以上這些用法去掉 is ,則變?yōu)榱藢?duì)應(yīng)的字符串轉(zhuǎn)發(fā)方法。學(xué)一套會(huì)兩套,買(mǎi)一送一....

最后說(shuō)一個(gè)不帶 . 的 is* --- isinstance(obj,type)

判斷一個(gè) object 是什么類(lèi)型...
type 可選類(lèi)型為:int,float,bool,complex,str,bytes,unicode,list,dict,set,tuple
并且 type 可以為一個(gè)原組:isinstance(obj, (str, int))

判斷字符串中的內(nèi)容

.*with() starts ends 不僅支持開(kāi)頭結(jié)尾的匹配,還支持 start 和 end 兩個(gè)參數(shù)來(lái)動(dòng)態(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 來(lái)判斷字符串的還有 .find()、.rfind() 和? .index()、.rindex()

這兩類(lèi)字符串尋址方法均支持從左到右、從右至左兩種尋址方式,不同的是:

find 在未找到時(shí),返回 -1,而 index 在未找到時(shí),會(huì)拋出 ValueError 的異常...

long_string.index('live') # 3long_string.rindex('live') # 42

字符串的內(nèi)容變更

狹義來(lái)說(shuō)使用,字符串的替換使用 .replace() 即可,那為什么還要單獨(dú)說(shuō)呢?因?yàn)樗幸粋€(gè)可選你參數(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'# 可以看到,第二個(gè)live并未進(jìn)行替換

剛才說(shuō)了狹義,那么廣義呢?

(l/r)strip()
將字符串左、右、兩端的特定字符過(guò)濾掉,默認(rèn)為空格...
strip() 要注意的地方是,strip('TolLive') 中的字符并非完整匹配,而是針對(duì)每一個(gè)字符進(jìn)行匹配,說(shuō)起來(lái)混,直接上例子:

long_string = "To live is to learn,to learn is to better live"long_string.strip('TolLive')'s to learn,to learn is to better'

字符串切片
字符串的切片分為 long_string[start:end;step] start、end 區(qū)間為左閉右開(kāi)...這個(gè)網(wǎng)上說(shuō)的太多了,再拉出來(lái)詳細(xì)講就要挨打了...

(l/r)just(width,[fillchar])、center(width, [fillchar])、zfill(width)
這些均為填充固定長(zhǎng)度的字符,默認(rèn)使用空格 ( zfill 為左補(bǔ) 0,z 是 zero 的意思...),看意思就明白了,不用多講了....

字符串格式化輸出

本來(lái) fill 和 center 等可以放在這里,但是他們使用頻率和重量級(jí)不夠格,就丟在上面了。

Python 格式化輸出分為兩類(lèi),那是在 pyton2 的時(shí)代,即 %? 和 format 。這兩種網(wǎng)上的資料太多了,說(shuō)的太多顯得沒(méi)逼格...
但,還是要簡(jiǎn)單說(shuō)說(shuō)其中特殊的地方

% 格式化輸出:

  • 如何在 % 的格式輸出中,輸出用來(lái)看做標(biāo)記為的 % 符號(hào)呢?使用兩個(gè)百分號(hào)(%%)

  • %(-)(width) width 為設(shè)置長(zhǎng)度,默認(rèn)左填充空格,添加 - 號(hào)為右填充

  • width 代表字符串截?cái)?#xff0c;保留多少長(zhǎng)度的字符串

  • type %s 字符串 %d 十進(jìn)制整數(shù) %f 小數(shù) ...

  • 多個(gè)參數(shù)是,后面的參數(shù)需要使用括號(hào)包裹起來(lái)

'姓名:%-5s 年齡:%4d 愛(ài)好:%.8s' % ('王大錘',30,'python、Java')output:'姓名:王大錘 年齡:30 愛(ài)好:python、J'

format 格式輸出:

format 在 python3 開(kāi)始官方就表示為替換 % 的輸出方式,之所以還保留著 %,主要是為了兼容性考慮...

  • 對(duì)比 %,format 使用花括號(hào) {} 表示變量

  • < > ^? 代表了 format 的對(duì)齊方式

'{:-^40s}'.format('華麗的分割線')output:'-----------------華麗的分割線-----------------'

f-string

Python3.6 的版本更新時(shí),新增了 f-string,英文好的可以去看官方解釋 PEP 498 -- Literal String Interpolation 。

f-string 是字符串引號(hào)前以 f/F 開(kāi)頭,并使用 {} 標(biāo)注替換位置的使用形式。

之所以官方推出 f-string,主要是因?yàn)樗母叩男阅堋⒏鼜?qiáng)的功能。例子走起:

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)載 | 寫(xiě)幾個(gè) Python 進(jìn)階必備函數(shù)

?

創(chuàng)智俱樂(lè)部

微信:sziitlSA

一個(gè)讓你漲姿勢(shì)的社團(tuán)

長(zhǎng)按二維碼關(guān)注

總結(jié)

以上是生活随笔為你收集整理的amigo幸运字符什么意思_转载 | 史上最全 python 字符串操作指南的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。