Python中String模塊示例詳解
Python中String模塊詳解
一、 字符串常量
String庫中的內(nèi)置的所有常量:
源碼中的概括:
whitespace -- a string containing all ASCII whitespace ascii_lowercase -- a string containing all ASCII lowercase letters ascii_uppercase -- a string containing all ASCII uppercase letters ascii_letters -- a string containing all ASCII letters digits -- a string containing all ASCII decimal digits hexdigits -- a string containing all ASCII hexadecimal digits octdigits -- a string containing all ASCII octal digits punctuation -- a string containing all ASCII punctuation characters printable -- a string containing all ASCII characters considered printable
示例:
# -*- coding: utf-8 -*- """ Created on Sun Dec 18 18:58:35 2022 @author: Steve Anthony """ import string print(string.whitespace) # 包含所有的空格 print(string.ascii_lowercase) # 包含所有的小寫字母 print(string.ascii_uppercase) # 包含所有的大寫字母 print(string.ascii_letters) # 包含ASCII中的所有字母 print(string.digits) # 包含所有的數(shù)字字符串 print(string.hexdigits) # 包含所有的十六進(jìn)制字符字符串 print(string.octdigits) # 包含所有的八進(jìn)制字符字符串 print(string.punctuation) # 包含所有的標(biāo)點符號字符串 print(string.printable) # 包含所有可打印的ASCII字符字符串
二、 類
1、 格式化
1.1 介紹
String
模塊中,有一個Formatter
類,其可以對字符串進(jìn)行格式化。
該類中有一個format()
方法,和str.format()
方法使用方式類似,同時該類的主要作用就是使用format()
方法,對字符串進(jìn)行格式化輸出。
1.2 簡單應(yīng)用
print('{0}, {1}, {2}'.format('a', 'b', 'c')) print('{}, {}, {}'.format('a', 'b', 'c')) # 3.1+ only print('{2}, {1}, {0}'.format('a', 'b', 'c')) print('Coordinates: {latitude}, {longitude}'.format(latitude='37.24N', longitude='-115.81W'))
同時,也可以結(jié)合元組或者字典的解包來使用。
1.3 格式化輸出
>>> '{:<30}'.format('left aligned') # 向右對齊,保留30個字符,如果字符不夠使用空格填充 'left aligned ' >>> '{:>30}'.format('right aligned') # 向左對齊,保留30個字符,如果字符不夠使用空格填充 ' right aligned' >>> '{:^30}'.format('centered') # 居中對齊,保留30個字符,如果字符不夠使用空格填充 ' centered ' >>> '{:*^30}'.format('centered') # use '*' as a fill char '***********centered***********' >>> '{:+f}; {:+f}'.format(3.14, -3.14) # show it always '+3.140000; -3.140000' >>> '{: f}; {: f}'.format(3.14, -3.14) # show a space for positive numbers ' 3.140000; -3.140000' >>> '{:-f}; {:-f}'.format(3.14, -3.14) # show only the minus -- same as '{:f}; {:f}' '3.140000; -3.140000' >>> # format also supports binary numbers >>> "int: {0:d}; hex: {0:x}; oct: {0:o}; bin: {0:b}".format(42) 'int: 42; hex: 2a; oct: 52; bin: 101010' >>> # with 0x, 0o, or 0b as prefix: >>> "int: {0:d}; hex: {0:#x}; oct: {0:#o}; bin: {0:#b}".format(42) 'int: 42; hex: 0x2a; oct: 0o52; bin: 0b101010' >>> 'Correct answers: {:.2%}'.format(19/22) # 保留兩位小數(shù) 'Correct answers: 86.36%'
還可以用于對時間等特殊字符串的格式化
import datetime d = datetime.datetime(2010, 7, 4, 12, 15, 58) print('{:%Y-%m-%d %H:%M:%S}'.format(d))
2、 模板化
模板字符串提供了更簡單的字符串替換。因為在該上下文中,更簡單的語法和功能使其比 Python 中的其他內(nèi)置字符串格式設(shè)施更容易翻譯。
模板字符串支持基于$
的替換,使用以下規(guī)則:
- 使用
$$
進(jìn)行轉(zhuǎn)義,其代表$
本身 $Identity
命名一個替換占位符,該占位符與映射關(guān)鍵字“Identity
”匹配。默認(rèn)情況下,“標(biāo)識符”僅限于以下劃線或 ASCII 字母開頭的任何不區(qū)分大小寫的 ASCII 字母數(shù)字字符串(包括下劃線)。$字符之后的第一個非標(biāo)識符字符終止此占位符規(guī)范${identifier}
等價于$identifier
使用示例:
# -*- coding: utf-8 -*- """ Created on Sun Dec 18 18:58:35 2022 @author: Steve Anthony """ from string import Template s = Template('$who的年齡為:${age}') print(s.safe_substitute({"who": "李華", "age": 13})) # safe_*這個函數(shù)如果沒有給字符串里面的所有變量賦值不會報錯 print(s.safe_substitute(**{"who": "李華"})) print(s.substitute(**{"who": "李華", "age": 13})) # 但是這個函數(shù),必須要給字符串里面所有定義的變量都賦值,否則會報錯 print(s.substitute({"who": "李華"}))
三、 函數(shù)
對于String
的常用方法,可以去Python基礎(chǔ)語法里面學(xué)習(xí)
同時,有一個比較特殊的函數(shù)capwords(s, sep=" ")
,可以學(xué)習(xí)學(xué)習(xí)
作用,根據(jù)分隔符,將字符串分成幾塊,并且將每一塊字符串的第一個字母轉(zhuǎn)換為大寫字母(如果不是字符則不改變),其余字母轉(zhuǎn)換為小寫字母,最后使用分隔符拼接回去。
使用示例:
# -*- coding: utf-8 -*- """ Created on Sun Dec 18 18:58:35 2022 @author: Steve Anthony """ import string a = string.capwords("*hello python! my nAmE iS") print(a)
到此這篇關(guān)于Python中String模塊的文章就介紹到這了,更多相關(guān)Python中String模塊內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
python散點圖雙軸設(shè)置坐標(biāo)軸刻度的實現(xiàn)
散點圖是一種常用的圖表類型,可以用來展示兩個變量之間的關(guān)系,本文主要介紹了python散點圖雙軸設(shè)置坐標(biāo)軸刻度的實現(xiàn),具有一定的參考價值,感興趣的可以了解一下2024-01-01python趣味挑戰(zhàn)之爬取天氣與微博熱搜并自動發(fā)給微信好友
忙著畢設(shè)與打游戲之余,突然想著寫個爬蟲練練手,想了想,就寫了一個爬蟲爬取“中國天氣網(wǎng)”與“微博熱搜”并定時發(fā)送給微信好友,放到服務(wù)器上運行了幾天算是正常,需要的朋友可以參考下2021-05-05pytorch中關(guān)于distributedsampler函數(shù)的使用
這篇文章主要介紹了pytorch中關(guān)于distributedsampler函數(shù)的使用,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-02-02python爬取Ajax動態(tài)加載網(wǎng)頁過程解析
這篇文章主要介紹了python爬取Ajax動態(tài)加載網(wǎng)頁過程解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2019-09-09Pycharm終端顯示PS而不顯示虛擬環(huán)境名的解決
這篇文章主要介紹了Pycharm終端顯示PS而不顯示虛擬環(huán)境名的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-06-06非常詳細(xì)的Django連接mysql數(shù)據(jù)庫步驟記錄
我的Mysql中已經(jīng)有了項目需要使用的相關(guān)數(shù)據(jù)庫,現(xiàn)在需要通過django來獲取Mysql里的數(shù)據(jù)并使用,下面這篇文章主要給大家介紹了關(guān)于非常詳細(xì)的Django連接mysql數(shù)據(jù)庫步驟,需要的朋友可以參考下2022-10-10