Python學習筆記 字串練習及可變字串

2021-10-05 05:40:23 字數 1868 閱讀 2032

python內字串不允許修改,要修改用可變字串

可變字串練習

import io

s='hello,mike'

sio=io.stringio(s)

a=sio.getvalue(

)sio.seek(6)

#改變游標地方

sio.write(

'f')

sio.getvalue(

)print

(sio.getvalue(

))

執行結果

hello,fike
字串練習

import time

a='to be not to be'

print

(a.startswith(

'to'))

print

(a.endswith(

'be'))

print

(a.find(

't')

)print

(a.rfind(

't')

)print

(a.count(

't')

)print

(a.isalnum())

print

(a.strip(

't')

)print

(a.lstrip(

't')

)print

(a.rstrip(

'e')

)b=a.split(

'be'

)print

(b)c=

'*a*'

.join(b)

print

(c)d=a.capitalize(

)print

(d)e=a.title(

)print

(e)f=a.upper(

)print

(f)g=a.lower(

)print

(g)k=a.swapcase(

)print

(k)time01=time.time()h=

''for i in

range

(1000000):

h+='sxit'

time02=time.time(

)print

('運算時間'

+str

(time02-time01)

)time03=time.time(

)li=

for i in

range

(1000000):

'sxit')h=

''.join(li)

time04=time.time(

)print

('運算時間'

+str

(time04-time03)

)

執行結果

true

true010

3false

o be not to be

o be not to be

to be not to b

['to '

,' not to ',''

]to *a*

not to *a*

to be not to be

to be not to be

to be not to be

to be not to be

to be not to be

運算時間0.9697716236114502

運算時間0.14424657821655273

Python學習筆記 字串小練習

一 編寫乙個程式,接受一行序列作為輸入,並在將句子中的所有字元大寫後列印行。假設向程式提供以下輸入 hello world practice makes perfect 則輸出為 hello world practice makes perfect str1 input 請輸入乙個字串 print ...

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