Python之字串練習

2021-09-24 19:36:45 字數 1728 閱讀 5108

"""

給定乙個字串來代表乙個學生的出勤紀錄,這個紀錄僅包含以下三個

字元:'a' : absent,缺勤

'l' : late,遲到

'p' : present,到場

如果乙個學生的出勤紀錄中不超過乙個'a'(缺勤)並且不超過兩個連續的'l'(遲到),

那麼這個學生會被獎賞。

你需要根據這個學生的出勤紀錄判斷他是否會被獎賞。

示例 1:

輸入: "ppallp"

輸出: true

示例 2:

輸入: "ppalll"

輸出: false

"""

方法1

方法2

實現

替換字元

如they are students

aeiou

thy r stdnts

"""# 定義兩個接收使用者輸入的變數

str1 = input('str1:')

str2 = input('str2:')

# 遍歷整個字串

for i in str1:

for j in str2:

if i == j:

break

# end 指定結束符,在for語句體中表示不換行輸出

# 遍歷字串s1

for i in s1:

# 依次判斷s1中的每個字元是否在s2中

if i in s2:

# replace 表示替換;

# 將s1中與s2中相同的所有字元,替換為空字元

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