天天看點

python格式化字元記錄

跟練書目《笨辦法學python3》

案例1

topic = 'monkey'
print(f"let's talk about {topic}.") 
           

輸出

案例2

A = "apple"
B = "banana"
y = f"{A} and {B}"
print(y)
           

輸出

apple and banana
           

案例3

hilarious=False
joke_evaluation = "Isn't that joke so funny?! {}"
print(joke_evaluation.format(hilarious))
#"{}".format()是格式化輸出的另一種用法
           

輸出

Isn't that joke so funny?! False
           

案例4("{}".format()的多參數版本)

formatter="{} {} {} {}"
print(formatter.format(1,2,3,4))
print(formatter.format("one","two","three","four"))
print(formatter.format(True,False,True,False))
print(formatter.format(formatter,formatter,formatter,formatter))
print(formatter.format("try your","own text here","maybe a poem","or a song"))

           

輸出

1 2 3 4
one two three four
True False True False
{} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {}
try your own text here maybe a poem or a song