Python字串練習

2021-09-27 12:47:00 字數 1649 閱讀 6656

```python

mystr=

'hello world and dgjfkhgj'

#title():返回乙個首字母大寫的字串

print

(mystr.title())

#capitalize():返回第乙個首字母大寫的字串

print

(mystr.capitalize())

#upper()返回全部大寫

print

(mystr.upper())

#lower()返回全部小寫

print

(mystr.lower())

mystr1=

'hello world and dgjfkhgj'

print

(mystr1.lower())

#swapcase()用於字串的大小寫進行轉換

print

(mystr1.swapcase())

mystr2=

' hello'

#strip()去掉兩邊的空格

print

(mystr2.strip())

#center(20)返回乙個原字串居中,並使用空格填充至長度,width的新字串,預設填充字元為空格

print

(mystr2.center(20)

)mystr3=

'hello word.\nthis is liming.\nhe is a student.'

print

(mystr3)

newstr3=mystr3.splitlines(

)print

(newstr3)

#isalpha:判斷是否為字母,返回false或true

str=

'dgkfh'

print

(str

.isalpha())

#isdigit,isnumeric,isdecimal 判斷是否為數字

str1=

'fdkgjfkb'

print

(str1.isdigit())

str2=

'2409605687'

print

(str2.isdecimal())

#join:字串的拼接

li=[

"my"

,"name"

,"is"

,"lixiaohuan"

]print

(li)

sp=' '

new=sp.join(li)

print

(new)

#isalnum()判斷是否為字母或數字組成,只要有字母,就返回true,不能有標點符號或者空格(特殊符號)

str3=

'石家 莊'

print

(str3.isalnum())

str4=

'石家莊'

print

(str4.isalnum())

str5=

'vcjdlf'

#isspace()確認是否有空格

print

(str5.isspace(

))

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...

Python之字串練習

給定乙個字串來代表乙個學生的出勤紀錄,這個紀錄僅包含以下三個 字元 a absent,缺勤 l late,遲到 p present,到場 如果乙個學生的出勤紀錄中不超過乙個 a 缺勤 並且不超過兩個連續的 l 遲到 那麼這個學生會被獎賞。你需要根據這個學生的出勤紀錄判斷他是否會被獎賞。示例 1 輸入...