X = f"some stuff here {a variable}" Y = f"some other stuff {another var}" Print(f" {X} ")
eg.
hilarious = False joke_evaluation = "Isn't that joke so funny?! {}" print(joke_evaluation.format(hilarious))
輸出結果:
Isn't that joke so funny?! False
print("Its fleece was white as {}.".format("snow")) print(end1 + end2 + end3 + end4 + end5 + end6, end=" ")
end 是print 的一個參數,它的默認值的\n(換行)
end = " " 的作用是不換行,但是每個變量之間隔一個空格。
若是不想要空格也可以:
print(end1 + end2 + end3 + end4 + end5 + end6, end="") print(end1 + end2 + end3 + end4 + end5 + end6, end="#")
str.format( )方法通過字符串中的花括號{ }來識別替換字段(replacement field), 從而完成字符串的格式化。
知識拓展:
https://blog.csdn.net/jpch89/article/details/84099277
替換字段由字段名 field name 和轉換字段 conversion field 以及格式說明符 format specifier 組成, 即一般形式爲 {字段名!轉換字段: 格式說明符}
字段名分爲簡單字段名simple field name和複合字段名 compound field name
轉換字段和格式說明符都是可選的
簡單字段名:
複合字段名(同時使用了數字和變量名兩種形式的字段名)
\n 讀作:backslash n 換行
」」」 三個符號中間沒有空格
」」」這裏是多行輸出」」」
** Escape sequences 轉義符
\t tab in
符 號 說 明
’ 單引號 single quote
" 雙引號 double quote
\a 發出系統響鈴聲 system alarm bell
\b 退格符 backspace
\n 換行符 newline
\t 橫向製表符 tab in
\v 縱向製表符
\r 回車符 carriage
\f 換頁符 formfeed
\o 八進制數代表的字符
\x 十六進制數代表的字符
\000 終止符,\000後的字符串全部忽略