牛客網程式設計中的學習點
阿新 • • 發佈:2020-12-29
技術標籤:python
split函式中的分隔符預設為空格,可以用" "或者' '代替,但是不能用""或者'',否則會報語法錯
程式設計題目:字串最後一個單詞的長度
自己的程式碼:
str_origin = str(input()) if len(str_origin) < int(5000) and str_origin != '': str_list = str_origin.split('') print(len(str_list[-1])) else: print("This str is not legal!!!")
錯誤點:
錯誤原因
1:冒號寫成了中文冒號
2:split函式預設的f分割符就是空格,不需要畫蛇添足
正確程式碼:
count()函式用於計算字串A在某個字串B中出現的次數,在驗證自己輸入的資料時,如果涉及到字母,可以考慮字母大小寫格式化問題對測試是否有影響
程式設計題目:計算某字母出現的次數
自己的程式碼:
origin_str = str(input())
single_str = str(input())
print(origin_str.find(single_str))
錯誤點:
錯誤原因:
使用了錯誤的方法:
1.
1).find():返回子字串在目標字串中第一次出現的索引位置
2).count():返回子字串在目標字串中出現的次數
3).re.search():返回滿足匹配條件的第一個匹配項
4).re.match():和search相比,只匹配開始
2.未對輸入資料做格式化處理:
str.lower()
正確程式碼:
origin_str = str(input()).lower()
single_str = str(input()).lower()
print(origin_str.count(single_str))