<button id="nc8ni"><xmp id="nc8ni"><button id="nc8ni"></button><ins id="nc8ni"><button id="nc8ni"><form id="nc8ni"></form></button></ins>
<button id="nc8ni"><form id="nc8ni"></form></button>
<form id="nc8ni"><xmp id="nc8ni">
<ins id="nc8ni"><button id="nc8ni"></button></ins>
<button id="nc8ni"><xmp id="nc8ni">
<ins id="nc8ni"><form id="nc8ni"><ins id="nc8ni"></ins></form></ins>
<button id="nc8ni"><xmp id="nc8ni">
<button id="nc8ni"><xmp id="nc8ni">
<button id="nc8ni"><form id="nc8ni"><ins id="nc8ni"></ins></form></button><button id="nc8ni"><form id="nc8ni"></form></button>
<button id="nc8ni"><form id="nc8ni"><ins id="nc8ni"></ins></form></button><button id="nc8ni"><xmp id="nc8ni"><button id="nc8ni"></button><form id="nc8ni"><ins id="nc8ni"><form id="nc8ni"></form></ins></form><button id="nc8ni"><xmp id="nc8ni"><ins id="nc8ni"></ins>
<button id="nc8ni"><xmp id="nc8ni"><ins id="nc8ni"></ins><form id="nc8ni"><ins id="nc8ni"><form id="nc8ni"></form></ins></form>
<ins id="nc8ni"><form id="nc8ni"></form></ins>
<button id="nc8ni"><form id="nc8ni"><ins id="nc8ni"></ins></form></button>
<button id="nc8ni"></button>
<button id="nc8ni"></button>
訂閱本欄目 RSS您所在的位置: 深山工作室 > python > 正文

python中 f 用法

2022/9/14 14:57:56 字體: 瀏覽 9856

python中f‘{}‘用法
python的字符串前面加f表示格式化字符串,加f后可以在字符串里面使用用花括號括起來(lái)的變量和表達式,如果字符串里面沒(méi)有表達式,那么前面加不加f輸出應該都一樣。

Python3.6新增了一種f-字符串格式化
格式化的字符串文字前綴為’f’和接受的格式字符串相似str.format()。它們包含由花括號包圍的替換區域。替換字段是表達式,在運行時(shí)進(jìn)行評估,然后使用format()協(xié)議進(jìn)行格式化。
formatted string literals, 以 f 開(kāi)頭,包含的{}表達式在程序運行時(shí)會(huì )被表達式的值代替。

如:

f'名稱(chēng): {i["value"]}'

python3.6增加的方法,字符串定義以f開(kāi)頭,可以使用{}包裹變量,方便字符串的定義。
print(f'features:,{features.shape},\n,labels.shape:,{labels.shape}')
print(f'features:,{features.shape} \n labels.shape:,{labels.shape}')#不加逗號也可以。
print(f'epoch{epoch+1},' f'loss:{d2l.evaluate_loss(net,train_iter,loss):f}')#默認保留小數點(diǎn)后6位有效數字
print(f'epoch{epoch+1},' f'loss:{d2l.evaluate_loss(net,train_iter,loss):.5f}')#保留小數點(diǎn)后5位有效數字

python的print字符串前面加f表示格式化字符串,加f后可以在字符串里面使用用花括號括起來(lái)的變量和表達式,如果字符串里面沒(méi)有表達式,那么前面加不加f輸出應該都一樣.Python3.6新增了一種f-字符串格式化.
格式化的字符串文字前綴為’f’和接受的格式字符串相似str.format()。它們包含由花括號包圍的替換區域。替換字段是表達式,在運行時(shí)進(jìn)行評估,然后使用format()協(xié)議進(jìn)行格式化。
formatted string literals, 以 f 開(kāi)頭,包含的{}表達式在程序運行時(shí)會(huì )被表達式的值代替。


#先定義一個(gè)類(lèi)
class Desk():
def desk(self):
print(‘能放東西’)
print(self)
#再定義一個(gè)對象
desk1 = Desk()
desk1.width1 = 400
desk1.height = 500

#當不用f’{}‘時(shí)候
print(‘桌子的寬度是{width1},桌子高度是{height}厘米’.format(width1=desk1.width1,height=desk1.height))
#當用f’{}‘時(shí)候
print(f’桌子的寬度是{desk1.width1}厘米’)
print(f’桌子高度是{desk1.height}厘米’)

#當不用f’{}'時(shí)候輸出打印信息如下
桌子的寬度是400,桌子高度是500厘米

#當用f’{}'時(shí)候輸出打印信息如下
桌子的寬度是400厘米
桌子高度是500厘米

總結:
f’{}’ 用法等同于 format用法的簡(jiǎn)單使用,更加方便

相關(guān)閱讀
利用CSS讓網(wǎng)頁(yè)里的提交按鈕變得更漂亮
07 問(wèn)題錯誤整理 程序目錄權限 父路徑?jīng)]打開(kāi)錯誤
帶時(shí)鐘的臺歷:一個(gè)非常非常漂亮的臺歷
uni-app用map組件建立微信小程序顯示商家地圖位置并且打上標志然后點(diǎn)標志進(jìn)行調用手機地圖導航到對應的地址
jquery獲得當前所有input表單數據并進(jìn)行序列化的各種問(wèn)題與解決方案serialize與serializeArray的區別分析
CSS布局實(shí)例:上中下三行,中間自適應
如何徹底卸載電腦里你不想用的軟件
極酷的,漂亮的頁(yè)面loading等待頁(yè)面特效(2)
共有0條關(guān)于《python中 f 用法》的評論
發(fā)表評論
正在加載評論......
返回頂部發(fā)表評論
呢 稱(chēng):
表 情:
內 容:
評論內容:不能超過(guò) 1000 字,需審核,請自覺(jué)遵守互聯(lián)網(wǎng)相關(guān)政策法規。
驗證碼: 驗證碼 
網(wǎng)友評論聲明,請自覺(jué)遵守互聯(lián)網(wǎng)相關(guān)政策法規。

您發(fā)布的評論即表示同意遵守以下條款:
一、不得利用本站危害國家安全、泄露國家秘密,不得侵犯國家、社會(huì )、集體和公民的合法權益;
二、不得發(fā)布國家法律、法規明令禁止的內容;互相尊重,對自己在本站的言論和行為負責;
三、本站對您所發(fā)布內容擁有處置權。

更多信息>>欄目類(lèi)別選擇
百度小程序開(kāi)發(fā)
微信小程序開(kāi)發(fā)
微信公眾號開(kāi)發(fā)
uni-app
asp函數庫
ASP
DIV+CSS
HTML
python
更多>>同類(lèi)信息
python通過(guò)代碼修改pip下載源讓下載庫飛起
python里面requests.post返回的res.text還有其它的嗎
windows7環(huán)境下安裝配置jdk
python對微信操作要用到這兩個(gè)庫wxpy與itchat
8種Python字符串拼接的方法,你知道幾種
Python拼接字符串的幾種方式
更多>>最新添加文章
抖音直播音掛載小雪花 懂車(chē)帝小程序
javascript獲取瀏覽器指紋可以用來(lái)做投票
火狐Mozilla Firefox出現:無(wú)法載入您的Firefox配置文件 它可能已經(jīng)丟失 或是無(wú)法訪(fǎng)問(wèn) 問(wèn)題解決集合處理辦法
在A(yíng)ndroid、iOS、Windows、MacOS中微信小程序的文件存放路徑
python通過(guò)代碼修改pip下載源讓下載庫飛起
python里面requests.post返回的res.text還有其它的嗎
aliyun阿里云續費域名優(yōu)惠口令(注冊、續費都可以使用)
windows7環(huán)境下安裝配置jdk
更多>>隨機抽取信息
網(wǎng)頁(yè)調用msn/qq/email記錄,淘寶,skype掛在網(wǎng)頁(yè)上
asp版圖形驗證碼,可自定義點(diǎn)陣信息,支持字符扭曲、傾斜、傾斜
JS+CSS實(shí)現圖片放大預覽效果(購物時(shí)放在產(chǎn)品上面預覽圖片)
網(wǎng)站改進(jìn)中
向各位網(wǎng)友匯報下許愿墻進(jìn)程。
javascript_vb_asp代碼轉換器
亚洲精品456在线|S级爆乳玩具酱国产VIP皮裤|欧美成人精品第一区二区三区|久久永久免费人妻精品我不卡|亚洲欧洲日产无码
<button id="nc8ni"><xmp id="nc8ni"><button id="nc8ni"></button><ins id="nc8ni"><button id="nc8ni"><form id="nc8ni"></form></button></ins>
<button id="nc8ni"><form id="nc8ni"></form></button>
<form id="nc8ni"><xmp id="nc8ni">
<ins id="nc8ni"><button id="nc8ni"></button></ins>
<button id="nc8ni"><xmp id="nc8ni">
<ins id="nc8ni"><form id="nc8ni"><ins id="nc8ni"></ins></form></ins>
<button id="nc8ni"><xmp id="nc8ni">
<button id="nc8ni"><xmp id="nc8ni">
<button id="nc8ni"><form id="nc8ni"><ins id="nc8ni"></ins></form></button><button id="nc8ni"><form id="nc8ni"></form></button>
<button id="nc8ni"><form id="nc8ni"><ins id="nc8ni"></ins></form></button><button id="nc8ni"><xmp id="nc8ni"><button id="nc8ni"></button><form id="nc8ni"><ins id="nc8ni"><form id="nc8ni"></form></ins></form><button id="nc8ni"><xmp id="nc8ni"><ins id="nc8ni"></ins>
<button id="nc8ni"><xmp id="nc8ni"><ins id="nc8ni"></ins><form id="nc8ni"><ins id="nc8ni"><form id="nc8ni"></form></ins></form>
<ins id="nc8ni"><form id="nc8ni"></form></ins>
<button id="nc8ni"><form id="nc8ni"><ins id="nc8ni"></ins></form></button>
<button id="nc8ni"></button>
<button id="nc8ni"></button>