您好,登錄后才能下訂單哦!
這期內(nèi)容當(dāng)中小編將會給大家?guī)碛嘘P(guān)使用python怎么移除字符串尾部的數(shù)字,文章內(nèi)容豐富且以專業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。
t.rstrip(string.digits)
這樣就全部將數(shù)字移除了,順便將string這個模塊看了下文檔,也有一定的收獲。
>>> import string >>> string.digits '0123456789' >>> string.hexdigits '0123456789abcdefABCDEF' >>> string.octdigits '01234567' >>> string.letters 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ' >>> string.lowercase 'abcdefghijklmnopqrstuvwxyz' >>> string.uppercase 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' >>> string.printable '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~ \t\n\r\x0b\x0c' >>> string.punctuation '!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~' >>> string.whitespace '\t\n\x0b\x0c\r ' >>>
同時string可以將字符串和int,float相互轉(zhuǎn)化:
>>> string.atof("1.23") 1.23 >>> string.atof("1") 1.0
轉(zhuǎn)換的時候還可以制定進(jìn)制的轉(zhuǎn)化
>>> string.atoi("20") 20 >>> string.atoi("20",base=10) 20 >>> string.atoi("20",base=16) 32 >>> string.atoi("20",base=8) 16 >>> string.atoi("20",base=2) Traceback (most recent call last): File "", line 1, in <module> File "/usr/lib64/python2.6/string.py", line 403, in atoi return _int(s, base) ValueError: invalid literal for int() with base 2: '20' >>> string.atoi("101",base=2) 5 >>> string.atoi("101",base=6) 37
capwords(s, sep = None)以sep作為分隔符,分割字符串是s,然后將每個字符串的首字母大寫
>>> string.capwords("this is a dog") 'This Is A Dog' >>> string.capwords("this is a dog",sep=" ") 'This Is A Dog' >>> string.capwords("this is a dog",sep="s") 'This is a dog' >>> string.capwords("this is a dog",sep="o") 'This is a doG' >>>
maketrans(s, r)創(chuàng)建一個s到r的轉(zhuǎn)換列表,然后可以使用translate()方法來實現(xiàn)
>>> replist=string.maketrans("123","abc") >>> replist1=string.maketrans("456","xyz") >>> s="123456789" >>> s.translate(replist) 'abc456789' >>> s.translate(replist1) '123xyz789'
1、云計算,典型應(yīng)用OpenStack。2、WEB前端開發(fā),眾多大型網(wǎng)站均為Python開發(fā)。3.人工智能應(yīng)用,基于大數(shù)據(jù)分析和深度學(xué)習(xí)而發(fā)展出來的人工智能本質(zhì)上已經(jīng)無法離開python。4、系統(tǒng)運維工程項目,自動化運維的標(biāo)配就是python+Django/flask。5、金融理財分析,量化交易,金融分析。6、大數(shù)據(jù)分析。
上述就是小編為大家分享的使用python怎么移除字符串尾部的數(shù)字了,如果剛好有類似的疑惑,不妨參照上述分析進(jìn)行理解。如果想知道更多相關(guān)知識,歡迎關(guān)注億速云行業(yè)資訊頻道。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。