網站首頁 編程語言 正文
Python中String模塊詳解
一、 字符串常量
String庫中的內置的所有常量:
源碼中的概括:
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) # 包含所有的數字字符串 print(string.hexdigits) # 包含所有的十六進制字符字符串 print(string.octdigits) # 包含所有的八進制字符字符串 print(string.punctuation) # 包含所有的標點符號字符串 print(string.printable) # 包含所有可打印的ASCII字符字符串
二、 類
1、 格式化
1.1 介紹
String
模塊中,有一個Formatter
類,其可以對字符串進行格式化。
該類中有一個format()
方法,和str.format()
方法使用方式類似,同時該類的主要作用就是使用format()
方法,對字符串進行格式化輸出。
1.2 簡單應用
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'))
同時,也可以結合元組或者字典的解包來使用。
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) # 保留兩位小數 '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 中的其他內置字符串格式設施更容易翻譯。
模板字符串支持基于$
的替換,使用以下規則:
- 使用
$$
進行轉義,其代表$
本身 -
$Identity
命名一個替換占位符,該占位符與映射關鍵字“Identity
”匹配。默認情況下,“標識符”僅限于以下劃線或 ASCII 字母開頭的任何不區分大小寫的 ASCII 字母數字字符串(包括下劃線)。$字符之后的第一個非標識符字符終止此占位符規范 -
${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_*這個函數如果沒有給字符串里面的所有變量賦值不會報錯 print(s.safe_substitute(**{"who": "李華"})) print(s.substitute(**{"who": "李華", "age": 13})) # 但是這個函數,必須要給字符串里面所有定義的變量都賦值,否則會報錯 print(s.substitute({"who": "李華"}))
三、 函數
對于String
的常用方法,可以去Python基礎語法里面學習
同時,有一個比較特殊的函數capwords(s, sep=" ")
,可以學習學習
作用,根據分隔符,將字符串分成幾塊,并且將每一塊字符串的第一個字母轉換為大寫字母(如果不是字符則不改變),其余字母轉換為小寫字母,最后使用分隔符拼接回去。
使用示例:
# -*- 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)
原文鏈接:https://blog.csdn.net/qq_62789540/article/details/128365500
相關推薦
- 2023-07-16 spring boot多模塊打包 運行
- 2022-08-31 ASP.NET?Core的日志系統介紹_實用技巧
- 2022-08-29 .NET?Core自定義配置文件_實用技巧
- 2022-10-17 Go如何優雅的使用字節池示例詳解_Golang
- 2022-04-15 ASP.NET?Core基礎之中間件_基礎應用
- 2022-07-23 C++深入細致探究二叉搜索樹_C 語言
- 2022-03-16 淺析ORB、SURF、SIFT特征點提取方法以及ICP匹配方法_C 語言
- 2023-03-16 ProxyWidget和Element更新的正確方式詳解_Android
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支