python從入門到實踐筆記

2021-09-24 22:35:26 字數 949 閱讀 4587

1.字串的一些方法,title()首字母大寫顯示單詞,upper()大寫,lower()轉化為小寫,儲存資料時,此方法很有用,一條原則,一般將字串轉換為小寫,儲存資料

weather = 'cold'

print(weather.title())

print(weather.upper())

print(weathe.lower())

2.使用「+」合併字串

weather = 'cold'

print('today is '+weather+'!')

3.空格 新增 \t 新增製表符,刪除字串開頭與末尾多餘的空白strip(),刪除開頭空白lstrip(),刪除末尾空白rstrip(),replace()

passage = ' 我 的 前 後 都 有 空 格 '

a='a b c '

b = a.split()

print(passage)

print(passage.rstrip())

print(passage.lstrip())

print(passage.strip())

print(passage.replace(' ',''))#replace(old,new,count)

4.兩個**表示乘方

>>100 ** 2

10000

>>2 ** 2

4>>2 ** 3

8#『**』直接放在print()有問題

5.由str()引發的思考,當數字型別左右有字串相連時,python無法判斷數字是用來當做數字使用還是字元使用

year = 23

print('i`am'+year )#錯誤輸出,需改為str(year)

python從入門到實踐

1.類,例項化是根據類來建立物件 類的實踐 建立乙個人類 class humans 3.7版本不加括號 def init self,name,height 血的教訓init前後兩條下劃線,self不是類的關鍵字 self.name name self.self.height height def l...

python從入門到實踐

1.json初探 import json json模組初探 json在pytho之間分享資料 json.dump a,b 要儲存的資料和用於儲存資料的檔案物件 json.load 將資料載入記憶體 number 1,2,3,4,5,6,7 filename number.json 副檔名.json指...

《Python程式設計從入門到實踐》學習筆記

一 變數的命名和使用 附 python關鍵字和內建函式 關鍵字 false class finally is return none continue for lambda try true def nonlocal while and del global not with as elif if ...