最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Python中f-string字符串格式化的使用

 更新時(shí)間:2025年05月07日 09:48:07   作者:shyoutou  
本文介紹了Python中f-string字符串格式化的基本用法、嵌入表達(dá)式、格式化輸出等,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧

一、基本用法

name = "Alice"
 age = 25
 ?
 # 使用 f-string 嵌入變量
 message = f"My name is {name} and I am {age} years old."
 print(message)
 ?
 # 輸出 My name is Alice and I am 25 years old.

二、嵌入表達(dá)式

a = 5
 b = 10
 ?
 # 使用 f-string 嵌入表達(dá)式
 result = f"The sum of {a} and  is {a + b}."
 print(result)
 ?
 # 輸出 The sum of 5 and 10 is 15.

三、格式化輸出

1、數(shù)字格式化

1.1浮點(diǎn)數(shù)格式化

語(yǔ)法:{value:.nf},其中 n 是保留的小數(shù)位數(shù)。

pi = 3.141592653589793
 ?
 # 保留兩位小數(shù)
 formatted_pi = f"Pi rounded to 2 decimal places: {pi:.2f}"
 print(formatted_pi)
 ?
 #輸出 Pi rounded to 2 decimal places: 3.14

1.2整數(shù)補(bǔ)零

語(yǔ)法:{value:0nd},其中 n 是總位數(shù),不足的部分用 0 填充。

number = 42
 ?
 # 補(bǔ)零到 5 位
 formatted_number = f"The number is {number:05d}"
 print(formatted_number)
 ?
 #輸出 The number is 00042

1.3千位分隔符

語(yǔ)法:{value:,},用逗號(hào)分隔千位。

population = 1234567890
 ?
 # 添加千位分隔符
 formatted_population = f"The world population is {population:,}"
 print(formatted_population)
 ?
 #輸出 The world population is 1,234,567,890

1.4百分比格式化

語(yǔ)法:{value:.n%},其中 n 是保留的小數(shù)位數(shù)。

ratio = 0.4567
 ?
 # 格式化為百分比,保留兩位小數(shù)
 formatted_ratio = f"The ratio is {ratio:.2%}"
 print(formatted_ratio)
 ?
 #輸出 The ratio is 45.67%

2、字符串格式化

2.1對(duì)齊和填充

語(yǔ)法:{value:width} 或 {value:<width}、{value:>width}{value:^width},其中 width 是總寬度。

  1. <:左對(duì)齊
  2. >:右對(duì)齊
  3. ^:居中對(duì)齊
name = "Alice"
 ?
 # 左對(duì)齊,總寬度為 10,用 ' ' 填充
 formatted_name = f"Name: {name:<10}!"
 print(formatted_name)
 ?
 # 右對(duì)齊,總寬度為 10,用 '*' 填充
 formatted_name = f"Name: {name:*>10}!"
 print(formatted_name)
 ?
 # 居中對(duì)齊,總寬度為 10,用 '=' 填充
 formatted_name = f"Name: {name:=^10}!"
 print(formatted_name)
 ?
 # 輸出
 Name: Alice     !
 Name: *****Alice!
 Name: ==Alice===!

2.2截?cái)嘧址?/h4>

語(yǔ)法:{value:.n},其中 n 是保留的字符數(shù)。

long_text = "This is a very long string."
 ?
 # 截?cái)酁榍?10 個(gè)字符
 formatted_text = f"Truncated: {long_text:.10}"
 print(formatted_text)
 ?
 #輸出 Truncated: This is a

3、日期和時(shí)間格式化

3.1格式化日期

使用 strftime 方法結(jié)合 f-string 格式化日期。

from datetime import datetime
 ?
 now = datetime.now()
 ?
 # 格式化日期
 formatted_date = f"Today is {now:%Y-%m-%d}"
 print(formatted_date)
 ?
 #輸出 Today is 2023-10-05

3.2格式化時(shí)間

from datetime import datetime
 ?
 now = datetime.now()
 ?
 # 格式化時(shí)間
 formatted_time = f"The time is {now:%H:%M:%S}"
 print(formatted_time)
 ?
 #輸出 The time is 14:35:22

4、其他格式化

4.1科學(xué)計(jì)數(shù)法

語(yǔ)法:{value:.ne},其中 n 是保留的小數(shù)位數(shù)。

number = 1234567890
 ?
 # 科學(xué)計(jì)數(shù)法,保留兩位小數(shù)
 formatted_number = f"Scientific notation: {number:.2e}"
 print(formatted_number)
 ?
 #輸出 Scientific notation: 1.23e+09

4.2二進(jìn)制、八進(jìn)制、十六進(jìn)制

語(yǔ)法:

  • 二進(jìn)制:{value:b}
  • 八進(jìn)制:{value:o}
  • 十六進(jìn)制:{value:x}(小寫(xiě))或 {value:X}(大寫(xiě))
number = 255
 ?
 # 二進(jìn)制
 binary = f"Binary: {number:b}"
 print(binary)
 ?
 # 八進(jìn)制
 octal = f"Octal: {number:o}"
 print(octal)
 ?
 # 十六進(jìn)制
 hexadecimal = f"Hexadecimal: {number:x}"
 print(hexadecimal)
 ?
 #輸出
 Binary: 11111111
 Octal: 377
 Hexadecimal: ff

四、調(diào)用函數(shù)和方法

name = "alice"
 ?
 # 調(diào)用字符串的 title() 方法
 formatted_name = f"Hello, {name.title()}!"
 print(formatted_name)
 ?
 # 輸出 Hello, Alice!

字符串title方法:

  • 將所有單詞的首字母大寫(xiě)包括像it's中的's

  • 與capitalize() 的區(qū)別:capitalize() 僅將整個(gè)字符串的第一個(gè)單詞的首字母大寫(xiě),其余字母小寫(xiě)。

五、使用字典和列表

person = {"name": "Bob", "age": 30}
 ?
 # 訪問(wèn)字典中的值
 info = f"{person['name']} is {person['age']} years old."
 print(info)
 ?
 numbers = [1, 2, 3, 4, 5]
 ?
 # 訪問(wèn)列表中的元素
 summary = f"The first number is {numbers[0]} and the last number is {numbers[-1]}."
 print(summary)
 ?
 # 輸出 
 Bob is 30 years old.
 The first number is 1 and the last number is 5.

六、多行f-string

name = "Charlie"
 age = 35
 ?
 # 多行 f-string
 message = f"""
 Name: {name}
 Age: {age}
 """
 print(message)
 ?
 # 輸出
 Name: Charlie
 Age: 35

七、嵌套f-string

a = 5
 b = 10
 ?
 # 嵌套 f-string
 result = f"The sum of {a} and  is {f'{a + b}'}."
 print(result)
 ?
 # 輸出 The sum of 5 and 10 is 15.

到此這篇關(guān)于Python中f-string字符串格式化的使用的文章就介紹到這了,更多相關(guān)Python f-string字符串格式化內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家! 

相關(guān)文章

  • 詳解python中g(shù)roupby函數(shù)通俗易懂

    詳解python中g(shù)roupby函數(shù)通俗易懂

    這篇文章主要介紹了詳解python中g(shù)roupby函數(shù)通俗易懂,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-05-05
  • 一篇文章徹底搞懂Python類屬性和方法的調(diào)用

    一篇文章徹底搞懂Python類屬性和方法的調(diào)用

    對(duì)python?調(diào)用類屬性的方法詳解測(cè)試時(shí)候類的調(diào)用是經(jīng)常會(huì)用到的,下面這篇文章主要給大家介紹了關(guān)于Python類屬性和方法的調(diào)用的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2022-06-06
  • 在Django下創(chuàng)建項(xiàng)目以及設(shè)置settings.py教程

    在Django下創(chuàng)建項(xiàng)目以及設(shè)置settings.py教程

    今天小編就為大家分享一篇在Django下創(chuàng)建項(xiàng)目以及設(shè)置settings.py教程,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2019-12-12
  • Python?input輸入超時(shí)選擇默認(rèn)值自動(dòng)跳過(guò)問(wèn)題

    Python?input輸入超時(shí)選擇默認(rèn)值自動(dòng)跳過(guò)問(wèn)題

    這篇文章主要介紹了Python?input輸入超時(shí)選擇默認(rèn)值自動(dòng)跳過(guò)問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-02-02
  • 使用pandas的DataFrame的plot方法繪制圖像的實(shí)例

    使用pandas的DataFrame的plot方法繪制圖像的實(shí)例

    今天小編就為大家分享一篇使用pandas的DataFrame的plot方法繪制圖像的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2018-05-05
  • Python操作MySQL MongoDB Oracle三大數(shù)據(jù)庫(kù)深入對(duì)比

    Python操作MySQL MongoDB Oracle三大數(shù)據(jù)庫(kù)深入對(duì)比

    對(duì)于數(shù)據(jù)分析師來(lái)說(shuō),學(xué)習(xí)數(shù)據(jù)庫(kù)最重要的就是學(xué)習(xí)它們的查詢功能。這篇文章就以這個(gè)為切入點(diǎn),為大家講述如何用Python操作這3個(gè)數(shù)據(jù)庫(kù)
    2021-10-10
  • django之跨表查詢及添加記錄的示例代碼

    django之跨表查詢及添加記錄的示例代碼

    表查詢是重要的操作。這篇文章主要介紹了django之跨表查詢及添加記錄的示例代碼,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2018-10-10
  • Python輕松實(shí)現(xiàn)在PDF中插入頁(yè)眉頁(yè)腳

    Python輕松實(shí)現(xiàn)在PDF中插入頁(yè)眉頁(yè)腳

    在 PDF 中自動(dòng)添加頁(yè)眉頁(yè)腳,可以讓文檔更規(guī)范、更具識(shí)別度,本文將使用Spire.PDF for Python實(shí)現(xiàn)簡(jiǎn)單快速地處理 PDF 文件相關(guān)的各種任務(wù),需要的可以了解下
    2025-10-10
  • Pytest中skip skipif跳過(guò)用例詳解

    Pytest中skip skipif跳過(guò)用例詳解

    今天給大家?guī)?lái)的是關(guān)于Python的相關(guān)知識(shí),文章圍繞著Pytest中skip skipif跳過(guò)用例展開(kāi),文中有非常詳細(xì)的介紹及代碼示例,需要的朋友可以參考下
    2021-06-06
  • Python tkinter模塊彈出窗口及傳值回到主窗口操作詳解

    Python tkinter模塊彈出窗口及傳值回到主窗口操作詳解

    這篇文章主要介紹了Python tkinter模塊彈出窗口及傳值回到主窗口操作,結(jié)合實(shí)例形式分析了Python使用tkinter模塊實(shí)現(xiàn)的彈出窗口及參數(shù)傳遞相關(guān)操作技巧,需要的朋友可以參考下
    2017-07-07

最新評(píng)論

拉萨市| 玉门市| 东海县| 庐江县| 天峨县| 扶绥县| 韶山市| 湘潭市| 大名县| 南开区| 同德县| 永善县| 晴隆县| 噶尔县| 易门县| 安多县| 信阳市| 无锡市| 东乌| 冀州市| 富顺县| 古丈县| 巨野县| 信宜市| 木兰县| 炉霍县| 齐河县| 昭苏县| 彰武县| 汕头市| 三亚市| 青川县| 全椒县| 富源县| 余庆县| 禹州市| 邛崃市| 崇州市| 虹口区| 昭通市| 西华县|