一、字符串前加"f"
1. %
可以使用 % 格式化字符串。
c = (250, 250)
# 使用 % 格式化
s1 = "坐標為:%s" % c
# TypeError: not all arguments converted during string formatting
s1 = "坐標為:%s" % (c,) # '坐標為:(250, 250)'
# 使用 format 格式化
s2 = "坐標為:{}".format(c) # '坐標為:(250, 250)'
2.format
Python 2.6 引入 format 格式化字符串的方式。
str.format() 是對 %-formatting 的改進,替換字段使用大括號 {} 標記。
"Hello, {}. You are {}.".format(name, age)
可以通過引用索引來改變引入順序:
age = 100
name = 'Hider'
print("Hello {1}.You are {0}-{0}.".format(age, name))
# 'Hello Hider.You are 100-100.'
如果插入變量名稱,則會獲得額外傳遞對象的權限:
person = {'name':'Hider', 'age':100}
print("Hello {name}.You are {age}.".format(age=person['age'], name=person['name']))
# 'Hello Hider.You are 100.'
使用 *
可以針對列表進行解碼:
data = ["Hider", 100]
print("Name:{0}, Age:{1}.".format(*data))
# 'Name:Hider, Age:100.'
使用 ** 來使用字典完成巧妙技巧:
person = {'name':'Hider', 'age':100}
print("Hello {name}.You are {age}.".format(**person)) # 解開字典形成獨立形參
# 'Hello Hider.You are 100.'
填充與對齊
^、<、>
分別是居中、左對齊、右對齊,后面帶寬度參數。
:
后面帶填充的字符,只能是一個字符,默認空格填充。
#Python學習交流群:531509025
print("{:>10}".format('18'))# ' 18'
print("{:0>10}".format('18')) # '0000000018'
print("{:A>10}".format('18')) # 'AAAAAAAA18'
print("{:A^10}".format('18')) # 'AAAA18AAAA'
zfill 方法補充
zfill方法返回指定長度的字符串,原字符串右對齊,前面填充0,使用語法為:
str.zfill(width)
print("18".zfill(10)) # '0000000018'
精度與類型f
精度常跟類型f一起使用:
print("{:.2f}".format(3.1415926)) # '3.14'
# 小數點后2位 float類型
其他進制
b、d、o、x分別代表二進制、十進制、八進制、十六進制。
#Python學習交流群:531509025
print("{:b}".format(18)) # '10010'
print("{:d}".format(18)) # '18'
print("{:o}".format(18)) # '22'
print("{:x}".format(18)) # '12'
千位分隔符
print("{:,}".format(1234567890)) # '1,234,567,890'
str.format( )是一個升級版本,代碼易讀,但當處理多個參數和更長字符串時,非常冗長,每個變量都要指明。
使用字典的方式 .format(**dict)
解壓,並通過字符串中的鍵值引用。
3.Python 3.6 之 f'{}' —— 一種改進Python格式字符串的新方法
格式化字符串常量(formatted string literals)是 Python 3.6 新引入的一種字符串格式化方法,主要目的是使格式化字符串的操作更加簡便。
-
f-string在形式上是以 f 或者 F 修飾符引領的字符串(f'xxx' 或 F'xxx'),以大括號 {} 標明被替代的字段。
-
f-string本質上不是字符串產常量,而是一個在運行時運算求值的表達式。
# 創建
my_dict = {'name':'Hider',
'age':'100'}
print(f"My name is {my_dict['name']}, I'm {my_dict['age']}.")
# My name is Hider, I'm 100.
import time
t0 = time.time()
time.sleep(1)
name = 'processing'
# f支持在字符串內使用大括號{}的python表達式
print(f"{name} done in {time.time() - t0:.2f} s.")
# processing done in 1.00 s.
二、字符串前加"r"
r
的作用是去除轉義字符。
例如:\n
表示反斜杠字符 +
字母n
,而不是換行符。
以 r
開頭的字符,常用於正則表達式,對應re
模塊。
#Python學習交流群:531509025
str1 = 'input\n'
str2 = r'input\n'
print(str1) # input 並換行
print(str2) # input\n
三、字符串前加"b"
b
的作用是表示一個 bytes 對象。
網絡編程中,服務器和瀏覽器只認 bytes 類型數據。
例如:
response = b'<h1>Hello World!</h1>'
send 函數的參數和 recv 函數返回值都是 bytes 類型。
在 Python3 中,bytes 和 str 相互轉換方式:
str.encode('utf-8') # 編碼
bytes.decode('utf-8') # 解碼
四、字符串前加"u"
u
的作用是表示字符串以 Unicode 格式進行編碼。
一般用在中文字符串前面,防止因為源碼儲存格式問題,導致再次使用時出現亂碼。
例如:
u'我是中文字符串。'
結尾給大家推薦一個非常好的學習教程,希望對你學習Python有幫助!
Python基礎入門教程推薦:更多Python視頻教程-關注B站:Python學習者
https://www.bilibili.com/video/BV1LL4y1h7ny?share_source=copy_web
Python爬蟲案例教程推薦:更多Python視頻教程-關注B站:Python學習者
https://www.bilibili.com/video/BV1QZ4y1N7YA?share_source=copy_web