Python 第二節 第十三課

2021-10-09 19:10:15 字數 1445 閱讀 9383

[toc]

字串有很多常用方法, 我們需要熟悉. 我們通過**將這些方法彙總起來, 方便大家查閱. 希望大家針對每個方法都做一次測試.

len(string)                                     字串長度

string.startwith( "指定字串" )     以指定字串開頭

string.endwith( "指定字串" )       以指定字串結尾

string.find ( "指定字串" )             第一次出現指定字串的位置

strring.rfind ( "指定字串" )          最後一次出現指定字元的位置

string.count( "指定字串" )           指定字串出現了幾次

string.isallnum()                             所有字元全是字母或數字

我們可以通過 strip() 去除字串首尾指定資訊. 通過lstrip() 去除字串左邊的指定資訊, rstrip() 去除字串右邊的指定資訊.

程式設計中關於字串大小寫在轉換的情況, 經常遇到. 我們將相關方法彙總到這裡. 

string.capitalize()                            產生新的字串, 首字母大寫

string.title()                                     產生新的字串, 每個單詞都首字母大寫

string.upper()                                  產生新的字串, 所有字元全轉成大寫

string.lower()                                   產生新的字串, 所有字元全轉成小寫

string.swapcase()                            產生新的字串,所有字元大小寫轉換

center(), ljust(), rjust() 這三個函式用於對字串實現排版.

isalnum()                                          檢測是否為字母或數字

isalpha()                                           檢測字串是否只由字母組成(含漢字))

isdigit()                                              檢測字串是否只由數字組成

ispace()                                            檢測是否為空白符

isupper()                                            是否為大寫字母

islower()                                             是否為小寫字母

python第十三課

迭代器 l 1,2,3 索引 迴圈 for for i in l i for k in dic pass enumerate print dir 告訴我列表擁有的所有方法 print dir 告訴我列表擁有的所有方法 print dir 告訴我列表擁有的所有方法 print dir range 10...

第十三課 模組

第十三課 模組 在python中,乙個py檔案就是乙個模組,檔名為 py模組名則是 匯入模組可以引用模組中已經寫好的功能。1.import語句 檔名 foo.py x 1def get print x def change global x x 0要想在另外乙個py檔案中引用foo.py中的功能,需...

python基礎第十三課 元組

元組的建立方式 元組的遍歷 python內建的資料結構之一,是乙個不可變序列 t python hello 90 t python hello 90 t tuple hello python 123 t 10,t python 10,20 123 t 1 30 如果元組中的物件是可變物件,則可變物件...