Python编程基础:第二十七节 format输出Format
第二十七節(jié) format輸出Format
- 前言
- 實(shí)踐
前言
在前面的學(xué)習(xí)中我們已經(jīng)接觸過str.format()的輸出格式,本節(jié)中我們將進(jìn)一步學(xué)習(xí)字符串打印輸出的相關(guān)內(nèi)容,并通過一系列的小例子感受使用str.format()輸出的便捷。
實(shí)踐
我們先來定義兩個變量:
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()格式輸出需要用到{}占位符,只要保證{}的個數(shù)與變量個數(shù)相同并且變量出現(xiàn)的順序與我們預(yù)期的一致,那就能打印出我們想要的結(jié)果:
print("The {} jumped over the {}".format(animal, item)) >>> The cow jumped over the moon這個例子中一共用到了兩個變量,所以我們需要兩個{}占位符,同時第一個位置期望打印"cow"所以我們將變量animal放在第一個位置,第二個位置期望打印"moon",所以我們將變量item放在第二個位置。如果我們顛倒變量的順序,那打印的內(nèi)容也會隨之改變:
print("The {} jumped over the {}".format(item, animal)) >>> The moon jumped over the cow當(dāng)然我們也可以通過索引指定當(dāng)前占位符表示哪個變量,只需要將變量的位置索引填到{}占位符內(nèi)部即可:
print("The {0} jumped over the {1}".format(animal, item)) >>> The cow jumped over the moon不難發(fā)現(xiàn),變量animal的位置索引為0,所以{0}所代替的就是animal,而變量item的位置索引為1,那么{1}所代替的就是變量item。那么,我們改變占位符中的索引也會改變輸出結(jié)果哦:
print("The {1} jumped over the {0}".format(animal, item)) >>> The moon jumped over the cow顯然兩個變量交換位置了,這是因?yàn)檎嘉环械乃饕淖兞?#xff0c;通過指定位置索引的方式,我們可以將一個變量打印多次:
print("The {1} jumped over the {1}".format(animal, item)) >>> The moon jumped over the moon兩個占位符均指向位置為1的變量,所以兩個位置均使用變量item替換。那么我們是否可以像函數(shù)一樣在打印的時候直接指定變量名稱并為其賦值呢?其實(shí)是可以的:
print("The {animal} jumped over the {item}".format(animal="cow", item="moon")) >>> The cow jumped over the moon我們通過為變量賦值并在占位符內(nèi)部指定變量名稱同樣可以將變量內(nèi)容填放至占位符所在位置。我們通過改變占位符所代表變量的名稱就可以改變最終的輸出結(jié)果:
print("The {item} jumped over the {animal}".format(animal="cow", item="moon")) >>> The moon jumped over the cow可見,第一個占位符中填寫的變量名稱換為{item}之后,其代表的變量也發(fā)生了改變。就像使用索引一樣,我們也可以對不同位置指定相同的變量:
print("The {animal} jumped over the {animal}".format(animal="cow", item="moon")) >>> The cow jumped over the cow我們甚至可以將含有占位符的輸出格式賦值給一個變量,然后基于這個變量進(jìn)行進(jìn)一步的打印輸出:
text = "The {} jumped over the {}" print(text.format(animal, item)) >>> The cow jumped over the moon通過以上內(nèi)容不難發(fā)現(xiàn)使用str.format()格式輸出字符串,其輸出方式更加靈活多變。接下來我們介紹基于str.format()的格式輸出。我們先開辟一個新的變量name,并對該變量進(jìn)行打印輸出:
name = "Tom" print("Hello, my name is {}".format(name)) >>> Hello, my name is Tom我們可以指定在字符串輸出時,為變量預(yù)留10個字符的寬度:
print("Hello, my name is {:10}. Nice to meet you".format(name)) >>> Hello, my name is Tom . Nice to meet you可見,由于Tom只有3個字符寬度,所以產(chǎn)生了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對于數(shù)值輸出,我們可以指定保留的小數(shù)位數(shù):
number = 3.14159 print("The number pi is {:.3f}".format(number)) >>> The number pi is 3.142這里我們指定精確到小數(shù)點(diǎn)后3位,并進(jìn)行四舍五入。對于整數(shù)而言,我們也可以用多種方式對其進(jìn)行打印輸出:
number = 1000 print("The number is {:,}".format(number)) >>> The number is 1,000我們可以為其每3位添加一個逗號,也可以進(jìn)行進(jìn)制轉(zhuǎn)換,例如轉(zhuǎn)為二進(jìn)制:
print("The number is {:b}".format(number)) >>> The number is 1111101000轉(zhuǎn)為八進(jìn)制:
print("The number is {:o}".format(number)) >>> The number is 1750轉(zhuǎn)為十六進(jìn)制:
print("The number is {:x}".format(number)) >>> The number is 3e8以及使用科學(xué)計(jì)數(shù)法進(jìn)行表示:
print("The number is {:e}".format(number)) >>> The number is 1.000000e+03以上便是format輸出的全部內(nèi)容,感謝大家的收藏、點(diǎn)贊、評論。我們下一節(jié)將介紹隨機(jī)數(shù)(Random Numbers),敬請期待~
總結(jié)
以上是生活随笔為你收集整理的Python编程基础:第二十七节 format输出Format的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Python编程基础:第二十五节 arg
- 下一篇: Python编程基础:第二十八节 随机数