1. 程式人生 > >06《learn Python3 the hard way》一起來敲程式碼,每天進步一點點

06《learn Python3 the hard way》一起來敲程式碼,每天進步一點點

lesson 6 字串和文字

從今天開始打算每天學習《learn python3 the hard way》,並在部落格裡記錄筆記,希望能堅持下來,總共有52課,我記得,從第六課開始,以下是我今天學習到的:

原始碼:

types_of_people = 10
x = f'There are {types_of_people}types_of_people.'

binary = "binary"
y = f"Those who know {binary} and those who {do_not}"

print(x)
print(y)

print(f'I said {x}')
print(
f'I also said {y}') hilarious = False joke_evaluation = "Isn't that joke so funny?!{}" print(joke_evaluation.format(hilarious)) w = "This is the left side of ..." e = "a string with a right side."
  1. f+間接列印 先給變數賦值,再用f" “內寫入格式化的文字,再將變數插入所需位置(可插入多個變數),再將語句賦值給變數3,最後列印變數3。 f”{變數1}",f"{變數1},{變數2} 舉個栗子:

         years_old = 18
         x = f"I am {years_old} years old."
         print(x)
    
  2. f+直接列印 直接在print語句中插入變數 舉個栗子:

         years_old = 18
         print(f"I am {years_old} years old.")
    
  3. format+列印 定義一個變數1,再另一個語句裡留空的大括號賦值給變數2,列印時候利用.format()打印出來。 連起來其實就是字串用"{}"挖個坑.format(變數2) 再舉個栗子

       years_old = 18 
       x = "I am {} years old"
    print(x.format(years_old))