Python字串總結及練習筆記

2021-08-20 15:07:45 字數 2512 閱讀 1579

# x = "234567asdsdfghj"  #切片和索引

# print(x[2:-2])

# print(x[2])

# x = "hello"     #顯示字串長度,注意是從1開始

# print(len(x))

# x = "hello world asdf"  #返回長度為100的字串,並在右側填充0

# print(x.zfill(100))

# x = "hello world asdf"  #小寫變為大寫

# print(x.upper())

# x = "234567asdsdfghj"  #大寫變為小寫

# print(x.lower())

# x = "234567sdfghj"  #判斷是否含有至少乙個區分大小寫的字元,並且這些都是小寫

# # print(x.islower())

# x = "hello world"   #返回標題化字串

# print(x.title())

# x = "hello world"   #翻轉字串中的大小寫

# print(x.swapcase())

# x = "     hello world     "     #同時執行lstrip和rstrip,刪除兩邊

# print(x.strip())

# x = "hello world"   #檢測開頭或結尾

# print(x.startswith('hea'))

# x = "hello world"

# print(x.endswith('o',0,5))

# x = "234567asdsd\nfasdghjasd"  #以\n行分隔,返回乙個包含元素的列表

# print(x.splitlines(true))

# x = "234567asdsdfasdghjasd"  #以a分隔x,並可指定次數

# print(x.split('a',2))

# x = "234567asdsdfasdghjasd"  #替換字串,並且可指定次數

# print(x.replace('asd','abc',1))

# x = "234567asdsdfghj"  #以7為中間符,分割x

# print(x.partition('7'))

# x = "234567asdzzsdfghj"  #返回x中最大的字母(小寫)

# print((max(x)))

# x = "121   234567asdsdfghj"  #截掉x左邊的1

# print(x.lstrip('1'))

# x = "234567sdfghj"  #左對齊,並以*填充剩餘數量(20)

# print(x.ljust(20,'*'))

# x = "*"  #以x為分隔符重新生成y

# y = "abc"

# print(x.join(y))

# x = "asdf112321 gh123j"  #判斷是否首字元為大寫,其它為小寫

# print(x.istitle())

# x = "  "  #判斷是否只包含空格

# print(x.isspace())

# x = "234567f"  #判斷是否只包含*數字字元*

# print(x.isnumeric())

# x = "234567"  #判斷是否全為數字

# print(x.isdigit())

# x = "234567sdfghj"  #判斷是否全為十進位制數字

# print(x.isdecimal())

# x = "234567sdfghj"  #判斷是否全為字母

# print(x.isalpha())

# x = "234567sdfghj"  #判斷是否全為字母或數字

# print(x.isalnum())

# x = "hello world"   #index同find,但查不到,會返回異常!!!

# print(x.index('a'))

# x = "hello world"   #find查詢字串並返回索引值

# print(x.find('d'))

# x = "name:,age:,***:"  #format格式化字串

# print(x.format('chao','18','male'))

# x = "name:{},age:{},***:{}"

# print(x.format('chao','18','male'))

# x = "hello \tworld"   #\t tab符號

# print(x.expandtabs(100))

# x = "hello world"    #在指定範圍內,返回l的次數

# print(x.count('l',3,10))

# x = "hello world"    #中間插入字串,兩邊填充*

# print(x.center(30,'*'))

Python字串練習

python mystr hello world and dgjfkhgj title 返回乙個首字母大寫的字串 print mystr.title capitalize 返回第乙個首字母大寫的字串 print mystr.capitalize upper 返回全部大寫 print mystr.up...

python 字串練習

name gouguoq 移除name變數對應值的兩邊的空格,並輸出移除後的內容 print name.strip 判斷name變數對應的值是否以 go 開頭,並輸出結果 print name.startswith go 判斷name變數對應的值是否以 q 結尾,並輸出結果 print name.e...

Python字串練習

取得校園新聞的編號 方法一 s s 14 5 方法二 s s.rstrip html 9 方法三 s s.rstrip html split 1 截圖 產生python文件的 addr1 addr2 html addr addr1 addr2 print addr 產生校園新聞的一系列新聞頁 for...