python(3) 字串簡單操作

2021-10-18 10:49:19 字數 1478 閱讀 8592

1.輸入一行字元,程式設計統計其中有多少個單詞。假設單詞之間以空格隔開。

#1s=(

input

("輸入字串:"))

count=

0for i in s:if(

ord(i)

>=

97and

ord(i)

<=

122)or(

ord(i)

>=

65and

ord(i)

<=90)

: count=count+

1print

(count)

#2

s=input

("請輸入字串:"

)num,t,space,d=0,

0,0,

0for i in s:

if i.isdigit():

num=num+

1elif i.isalpha():

t=t+

1elif i==

' ':

space=space+

1else

: d=d+

1print

(num,t,space,d)

2.請輸入星期幾的第1個字母,用來判斷是星期幾,如果第1個字母一樣,則繼續判斷第2個字母,以此類推。

a=

input

("請輸入字母:"

)if a[0]

=='m'

:print

("星期一"

)elif a[0]

=='f'

:print

("星期五"

)elif a[0]

=='w'

:print

("星期三"

)elif a[0]

=='t'

:if a[1]

=='u'

:print

("星期二"

)elif a[1]

=='h'

:print

("星期四"

)elif a[0]

=='s'

:if a[1]

=='a'

:print

("星期六"

)elif a[1]

=='u'

:print

("星期日"

)

3.編寫乙個程式,計算字串中子串出現的次數。

string=

input

("請輸入字串:"

)count=

0sub=

input

("請輸入子串:"

)cot=string.count(sub)

print

('出現了%d次'

%cot)

python3 字串操作

auther aaron fan name my tname is age is print name.capitalize 這段話的首字母大寫 print name.count a 統計這段字串中一共有多少個a print name.casefold print name.center 50,一共...

python3 字串常用操作

name my name is lisi 首字母大寫 capitalize name.capitalize my name is lisi 字串全部大寫 upper name.upper my name is lisi 字串全部小寫 lower name.lower my name is lisi ...

python3字串相等 python3 字串

1 拼接 1 多個字串進行連線 連線符,必須左右資料型別一致 例 print hello world 結果 helloworld 例 print 5 world 結果 typeerror unsupported operand type s for int and str 2 多個相同字串連線 字串...