python练习题(3)--字符串及正则表达式的应用
1.輸入一行字符,統計字符串中包含數字的個數
str = input("請輸入一串字符:") a = {} for i in str:a[i] = str.count(i) print(a)2.已知字符串:"this is a test of python".??按要求執行以下操作:
(1)統計該字符串中字母s出現的次數
(2)取出子字符串"test"
(3)將字符串中每個單詞的第一個字母變成大寫, 輸出到顯示器。
(4)用兩種方式實現該字符串的倒敘輸出
str = "this is a test of python"print(str.count("s")) # (1)print(str.split()[3]) # (2)print(str.title()) # (3)s1 = str.split(" ") # (4) 第一種方法 for i in s1[::-1]:print(i, "", end="")print(" ".join(str.split(" ")[::-1])) #第二種方法3.給定一個字符串來代表一個學生的出勤記錄 ,這個記錄僅包含以下三個字符:
‘A’ : Absent ,缺勤? ? ? ?‘L’:Late,遲到? ? ??‘P’ : Present .到場
如果一個學生的出勤記錄中不超過一個A(缺勤)并且不超過兩個連續的L(遲到)那么這個學生會被獎賞。?你需要根據這個學生的出勤記錄判斷他是否會被獎賞。
示例1:? ? ?輸入: "PPALLP"輸出: True
示例2:? ? ?輸入: "PPALLL"輸出:False
A = "缺勤" L = "遲到" P = "到場" a = input("請輸入學生的出勤記錄:") if a.count("A") <= 1 and a.count("LLL") == 0:print("True") else:print("False")4.有一身份證號,判斷此為男還是女,(身份證為18位)
a = input("請輸入身份證號:") b = int(a[16:17]) if b % 2 == 0:print("男") else:print("女")5.如下字符串,01#張三#20-02#李四#30-03#王五#40……,解析每個人分數多少。樣式如下:
01 張三 20
02 李四 30
03 王五 40? ,并且計算總分。
6.檢測某一個標識符是否是python的合法標識符(請分別用字符串函數和正則表達式兩種方法完成)
# 方法一 s = input("請輸入字符串:") if s[0].isalpha() or s[0] == "_":for j in range(1, len(s)):if s[j].isalnum() == False and s[j] != "_":print("標識符非法")breakelse:print("標識符合法") else:print("標識符非法") # 方法二 while True:l = input('請輸入字符串:')if l == 'exit':breakif l[0].isalpha() or l[0] == '_':for i in l[1:]:if not (i.isalnum() or i == "_"):print('標識符不合法')breakelse:print('標識符合法')else:print('標識符不合法')7.隨機生成驗證碼:
????????很多網站的注冊登錄業務都加入了驗證碼技術,以區分用戶是人還是計算機,有效地防止刷票、論壇灌水、惡意注冊等行為。目前驗證碼的種類層出不窮,其生成方式也越來越復雜,常見的驗證碼是由大寫字母、小寫字母、數字組成的六位驗證碼。
import randoma = "" for i in range(6):b = random.randint(1, 3) # b=1,大寫字母,b=2,小寫字母,b=3,數字if b == 1: # A(65)-Z(90)c = random.randint(65, 90)a = a + chr(c)elif b == 2: # a(97)-z(122)c = random.randint(97, 122)a = a + chr(c)elif b == 3:c = random.randint(0, 9)a = a + str(c) print(a)8.編程匹配簡單的以www.開頭以.com結尾的web域名,例如www.baidu.com
import repatt = 'www.+\.com' m = re.match(patt, 'www.baidu.com') if m is not None: m.group() print(m)9.判斷某一個字符串是否符合如下規則:用一個空格分隔的任意一對單詞,比如名和姓,若符合,輸出字符串,否則輸出“匹配失敗”。
import rea = input("請輸入字符串:") for i in a:if re.match(r'\s', i):print("匹配成功")break else:print("匹配失敗")10.匹配一個點(.)和一個空格分開的一字母和一個單詞,如英國人名中的姓和名。
import res = "xian ming.xiao wang" re.findall('\w+', s) print(s)11.利用正則表達式匹配163郵箱地址,以檢測輸入的郵箱是否合法.
import rea = input("請輸入郵箱地址:") while True:b = re.match("^163\w", a)if b:print("郵箱地址合法")breakelse:print("郵箱地址不合法")break else:print("郵箱地址不合法")12.假設有一段英文,其中有單獨的字母“I”誤寫為“i”,請編寫程序進行糾正。例如,這一段英文是:?"i am a teacher,i am man, and i am 38 years old.I am not a businessman.",請編寫程序,將文中的i改為I,要求用戶兩種方法完成。
# 方法一 import rea = "i am a teacher,i am man, and i am 38 years old.I am not a businessman."b = re.compile(r"(?:[^\w]|\b)i(?:[^\w])") while True:c = b.search(a)if c:if c.start(0) != 0:a = a[:c.start(0) + 1] + "I" + a[c.end(0) - 1:]else:a = a[:c.start(0)] + "I" + a[c.end(0) - 1:]else:break print(a) # 方法二 a = "i am a teacher,i am man, and i am 38 years old.I am not a businessman." b = a.replace("i", "I") print(b)13.有一段英文文本,其中有單詞連續重復了2次,編寫程序檢查重復的單詞并只保留一個。例如文本內容為“This is is a desk.”,程序輸出為“This is a desk.”
a = "This is is a desk" b = re.compile(r"\b(\w+)(\s+\1){1,}\b") c = b.search(a) a = b.sub(c.group(1), a) print(a)14.判斷手機號所屬運營商
說到手機號大家并不陌生,一個手機號碼由11位數字組成,前3位表示網絡識別號,第4~7位表示地區編號,第8~11位表示用戶編號。因此,我們可以通過手機號前3位的網絡識別號辨別手機號所屬運營商。在我國手機號運營商有移動、聯通、電信,各大運營商的網絡識別號如表1所示。
表1 運營商和網絡識別號
| 運營商 | 號碼段 |
| 移動 | 134、135、136、137、138、139、147、148、150、151、152、157、158、 159、165、178、182、183、184、187、188、198 |
| 聯通 | 130、131、132、140、145、146、155、156、166、185、186、175、176 |
| 電信 | 133、149、153、180、181、189、177、173、174、191、199 |
本實例要求編寫程序,實現判斷輸入的手機號碼是否合法以及判斷其所屬的運營商的功能。
import retel = input("請輸入電話號碼:") if re.match(r"^1\d{10}", tel):if re.match(r"^13[4-9]\d{8}", tel) or \re.match(r"^14[78]\d{8}", tel) or \re.match(r"^15[012789]\d{8}", tel) or \re.match(r"^165\d{8}", tel) or \re.match(r"^178\d{8}", tel) or \re.match(r"^18[23478]\d{8}", tel) or \re.match(r"^198\d{8}", tel):print("中國移動")elif re.match(r"^13[0-2]\d{8}", tel) or \re.match(r"^14[056]\d{8}", tel) or \re.match(r"^15[56]\d{8}", tel) or \re.match(r"^166\d{8}", tel) or \re.match(r"^17[56]\d{8}", tel) or \re.match(r"^18[56]\d{8}", tel):print(" 中國聯通") else:print("中國電信 ")總結
以上是生活随笔為你收集整理的python练习题(3)--字符串及正则表达式的应用的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 微信小程序云函数服务器,微信小程序云函数
- 下一篇: 人脸生成卡通图片