1. 程式人生 > >learn python the hard way 習題6~10總結

learn python the hard way 習題6~10總結

習題6總結

定義字串:

名字 = 值

其他

你也可以用 {types_of_people}的方式把它放在任何字串中。

也就是說你可以在其他字串中新增{},然後前面加一個 f,可用print()進行直接列印。

f-string

特殊的字串型別:f-string
舉例:f" some stuff have {avariable}"

.format()格式化方式:

python 還有一種 使用 .format()語法的格式化方式:
.format()格式化方式:

joke_evaluation = "Isn't that joke so funnty?!{}"
print(joke_evaluation.format(hilarious))

習題7總結

一種連續列印一串字元的方式:* 和 +

print("." * 10)
print( end1 + end2...)

習題8總結

format()函式

format ="{} {} {} {}"
formatter.format(...)的意思

  1. 取定義的 formatter 字串
  2. 呼叫它的 format 函式
  3. 給 format 傳遞4個引數,這些引數和 formatter 中的 {} 匹配,相當於將引數傳遞給 format 這個命令。
  4. 在 formatter 上呼叫 format 的結果是一個新字串,其中的 {} 被4個變數替換掉了。

習題9 和習題10 總結

轉義字元

使用 \ 可以將難錄入的字元放到字串,這叫做轉義序列。

輸出能夠換行的兩種方法

  1. 使用轉義序列\n
  2. 使用三引號,在程式碼中換行的部分同時也在 terminal 中換行,你可以在一組三引號之間放圖任意多行文字。