日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

Python编程基础:第二十七节 format输出Format

發布時間:2025/4/5 python 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Python编程基础:第二十七节 format输出Format 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

第二十七節 format輸出Format

  • 前言
  • 實踐

前言

在前面的學習中我們已經接觸過str.format()的輸出格式,本節中我們將進一步學習字符串打印輸出的相關內容,并通過一系列的小例子感受使用str.format()輸出的便捷。

實踐

我們先來定義兩個變量:

animal = "cow" item = "moon"

如果我們想用字符串拼接的方式輸出"The cow jumped over the moon"這句話,那就需要用到下述代碼:

print("The "+animal+" jumped over the "+item) >>> The cow jumped over the moon

我們在前面提到過,使用str.format()格式輸出需要用到{}占位符,只要保證{}的個數與變量個數相同并且變量出現的順序與我們預期的一致,那就能打印出我們想要的結果:

print("The {} jumped over the {}".format(animal, item)) >>> The cow jumped over the moon

這個例子中一共用到了兩個變量,所以我們需要兩個{}占位符,同時第一個位置期望打印"cow"所以我們將變量animal放在第一個位置,第二個位置期望打印"moon",所以我們將變量item放在第二個位置。如果我們顛倒變量的順序,那打印的內容也會隨之改變:

print("The {} jumped over the {}".format(item, animal)) >>> The moon jumped over the cow

當然我們也可以通過索引指定當前占位符表示哪個變量,只需要將變量的位置索引填到{}占位符內部即可:

print("The {0} jumped over the {1}".format(animal, item)) >>> The cow jumped over the moon

不難發現,變量animal的位置索引為0,所以{0}所代替的就是animal,而變量item的位置索引為1,那么{1}所代替的就是變量item。那么,我們改變占位符中的索引也會改變輸出結果哦:

print("The {1} jumped over the {0}".format(animal, item)) >>> The moon jumped over the cow

顯然兩個變量交換位置了,這是因為占位符中的索引改變了,通過指定位置索引的方式,我們可以將一個變量打印多次:

print("The {1} jumped over the {1}".format(animal, item)) >>> The moon jumped over the moon

兩個占位符均指向位置為1的變量,所以兩個位置均使用變量item替換。那么我們是否可以像函數一樣在打印的時候直接指定變量名稱并為其賦值呢?其實是可以的:

print("The {animal} jumped over the {item}".format(animal="cow", item="moon")) >>> The cow jumped over the moon

我們通過為變量賦值并在占位符內部指定變量名稱同樣可以將變量內容填放至占位符所在位置。我們通過改變占位符所代表變量的名稱就可以改變最終的輸出結果:

print("The {item} jumped over the {animal}".format(animal="cow", item="moon")) >>> The moon jumped over the cow

可見,第一個占位符中填寫的變量名稱換為{item}之后,其代表的變量也發生了改變。就像使用索引一樣,我們也可以對不同位置指定相同的變量:

print("The {animal} jumped over the {animal}".format(animal="cow", item="moon")) >>> The cow jumped over the cow

我們甚至可以將含有占位符的輸出格式賦值給一個變量,然后基于這個變量進行進一步的打印輸出:

text = "The {} jumped over the {}" print(text.format(animal, item)) >>> The cow jumped over the moon

通過以上內容不難發現使用str.format()格式輸出字符串,其輸出方式更加靈活多變。接下來我們介紹基于str.format()的格式輸出。我們先開辟一個新的變量name,并對該變量進行打印輸出:

name = "Tom" print("Hello, my name is {}".format(name)) >>> Hello, my name is Tom

我們可以指定在字符串輸出時,為變量預留10個字符的寬度:

print("Hello, my name is {:10}. Nice to meet you".format(name)) >>> Hello, my name is Tom . Nice to meet you

可見,由于Tom只有3個字符寬度,所以產生了7個字符寬度的空格。除此之外,我們還可以指定變量的對齊方式:

print("Hello, my name is {:<10}. Nice to meet you".format(name)) >>> Hello, my name is Tom . Nice to meet you

以上是左對齊,我們還可以右對齊:

print("Hello, my name is {:>10}. Nice to meet you".format(name)) >>> Hello, my name is Tom. Nice to meet you

甚至還可以居中對齊:

print("Hello, my name is {:^10}. Nice to meet you".format(name)) >>> Hello, my name is Tom . Nice to meet you

對于數值輸出,我們可以指定保留的小數位數:

number = 3.14159 print("The number pi is {:.3f}".format(number)) >>> The number pi is 3.142

這里我們指定精確到小數點后3位,并進行四舍五入。對于整數而言,我們也可以用多種方式對其進行打印輸出:

number = 1000 print("The number is {:,}".format(number)) >>> The number is 1,000

我們可以為其每3位添加一個逗號,也可以進行進制轉換,例如轉為二進制:

print("The number is {:b}".format(number)) >>> The number is 1111101000

轉為八進制:

print("The number is {:o}".format(number)) >>> The number is 1750

轉為十六進制:

print("The number is {:x}".format(number)) >>> The number is 3e8

以及使用科學計數法進行表示:

print("The number is {:e}".format(number)) >>> The number is 1.000000e+03

以上便是format輸出的全部內容,感謝大家的收藏、點贊、評論。我們下一節將介紹隨機數(Random Numbers),敬請期待~

總結

以上是生活随笔為你收集整理的Python编程基础:第二十七节 format输出Format的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。