《笨辦法學Python》 習題6

2021-09-13 02:08:09 字數 1248 閱讀 4037

加分習題

系統:mac os 10.14

python: 2.7.10

版本:《笨辦法學python》(第四版)

x = "there are %d types of people." % 10

binary = "binary"

do_not = "don't"

y = "those who know %s and those who %s." %(binary, do_not) #第1個位置

print x

print y

print "i said: %r." % x #第2個位置

print "i also said: '%s'." % y #第3個位置

hilarious = false

joke_evaluation = "isn't that joke so funny?! %r"

print joke_evaluation % hilarious #第4個位置

w = "this is the left side of ..."

e = "a string with a right side."

print w + e #字串拼接

在字串 x 中放入乙個整數型別的變數,並將變數直接賦值為乙個整數數值

定義變數 binary

定義變數 do_not

在字串x中放入兩個字串型別的變數,並將兩個變數放在了()中

列印字串 x

列印字串 y

輸出字串,並將 x 以 %r 的格式放入字串中一併輸出

輸出字串,並將 y 以 %s 的格式放入字串中一併輸出,' '對格式化字元沒有影響

定義變數 hilarious

定義字串 joke_evaluation,可在字串中放入 %r 格式的變數

列印字串 joke_evaluation,並放入hilarious,這裡可以字串變數名後面直接寫出想要插入的值

定義字串 w

定義字串 e

將字串 w 和 e 拼接在一起,並列印出來

**位置標註在偽**中。

待定,歡迎討論。

這裡是python中字串的功能,兩個字串可以通過「+」進行拼接。

當使用加號運算子的時候會呼叫這個類的_ add() _函式,這個函式是每個類都有的,對於自定義的類,不重寫這個方法,+這個運算子就沒作用。

——引自:

笨辦法學Python 3 習題6

交作業啦 ex6.py 建立了乙個名叫types of people的變數,並將其設為等於10 types of people 10 建立了乙個名叫x的變數,並將其設為f string x f there are types of people.建立了乙個名叫binary的變數,並將其設為字串 bi...

《笨辦法學python》習題38 40

mystuff 然後作者又給出了乙個錯誤的情況 class thing object def test hi print hia thing a.test hello 錯誤原因是test 只可以接受乙個引數,卻給了兩個。也就是 a.test hello 實際上是test a,hello 從這裡開始到...

《笨辦法學Python》 習題3

加分習題 系統 mac os 10.14 python 2.7.10 版本 笨辦法學python 第四版 print i will now count my chickens print hens 25 30 6.0 print roosters 100 25 3 4 print now i wil...