日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > python >内容正文

python

python练习题(3)--字符串及正则表达式的应用

發(fā)布時間:2024/1/8 python 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python练习题(3)--字符串及正则表达式的应用 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

1.輸入一行字符,統(tǒng)計(jì)字符串中包含數(shù)字的個數(shù)

str = input("請輸入一串字符:") a = {} for i in str:a[i] = str.count(i) print(a)

2.已知字符串:"this is a test of python".??按要求執(zhí)行以下操作:

(1)統(tǒng)計(jì)該字符串中字母s出現(xiàn)的次數(shù)

(2)取出子字符串"test"

(3)將字符串中每個單詞的第一個字母變成大寫, 輸出到顯示器。

(4)用兩種方式實(shí)現(xiàn)該字符串的倒敘輸出

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.給定一個字符串來代表一個學(xué)生的出勤記錄 ,這個記錄僅包含以下三個字符:

A : Absent ,缺勤? ? ? ?‘L:Late,遲到? ? ??‘P : Present .到場

如果一個學(xué)生的出勤記錄中不超過一個A(缺勤)并且不超過兩個連續(xù)的L(遲到)那么這個學(xué)生會被獎賞。?你需要根據(jù)這個學(xué)生的出勤記錄判斷他是否會被獎賞。

示例1:? ? ?輸入: "PPALLP"輸出: True

示例2:? ? ?輸入: "PPALLL"輸出:False

A = "缺勤" L = "遲到" P = "到場" a = input("請輸入學(xué)生的出勤記錄:") 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……,解析每個人分?jǐn)?shù)多少。樣式如下:
01 張三 20
02 李四 30
03 王五 40? ,并且計(jì)算總分。

a = "01#張三#20-02#李四#30-03#王五#40" list1 = a.split("-") list2 = [item.split("#") for item in list1] print(list2) sum = 0 for item in list2:sum += int(item[2]) print(sum)

6.檢測某一個標(biāo)識符是否是python的合法標(biāo)識符(請分別用字符串函數(shù)和正則表達(dá)式兩種方法完成)

# 方法一 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("標(biāo)識符非法")breakelse:print("標(biāo)識符合法") else:print("標(biāo)識符非法") # 方法二 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('標(biāo)識符不合法')breakelse:print('標(biāo)識符合法')else:print('標(biāo)識符不合法')

7.隨機(jī)生成驗(yàn)證碼:

????????很多網(wǎng)站的注冊登錄業(yè)務(wù)都加入了驗(yàn)證碼技術(shù),以區(qū)分用戶是人還是計(jì)算機(jī),有效地防止刷票、論壇灌水、惡意注冊等行為。目前驗(yàn)證碼的種類層出不窮,其生成方式也越來越復(fù)雜,常見的驗(yàn)證碼是由大寫字母、小寫字母、數(shù)字組成的六位驗(yàn)證碼。

import randoma = "" for i in range(6):b = random.randint(1, 3) # b=1,大寫字母,b=2,小寫字母,b=3,數(shù)字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結(jié)尾的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.判斷某一個字符串是否符合如下規(guī)則:用一個空格分隔的任意一對單詞,比如名和姓,若符合,輸出字符串,否則輸出“匹配失敗”。

import rea = input("請輸入字符串:") for i in a:if re.match(r'\s', i):print("匹配成功")break else:print("匹配失敗")

10.匹配一個點(diǎn)(.)和一個空格分開的一字母和一個單詞,如英國人名中的姓和名。

import res = "xian ming.xiao wang" re.findall('\w+', s) print(s)

11.利用正則表達(dá)式匹配163郵箱地址,以檢測輸入的郵箱是否合法.

import rea = input("請輸入郵箱地址:") while True:b = re.match("^163\w", a)if b:print("郵箱地址合法")breakelse:print("郵箱地址不合法")break else:print("郵箱地址不合法")

12.假設(shè)有一段英文,其中有單獨(dú)的字母“I”誤寫為“i”,請編寫程序進(jìn)行糾正。例如,這一段英文是:?"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.有一段英文文本,其中有單詞連續(xù)重復(fù)了2次,編寫程序檢查重復(fù)的單詞并只保留一個。例如文本內(nèi)容為“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.判斷手機(jī)號所屬運(yùn)營商

說到手機(jī)號大家并不陌生,一個手機(jī)號碼由11位數(shù)字組成,前3位表示網(wǎng)絡(luò)識別號,第4~7位表示地區(qū)編號,第8~11位表示用戶編號。因此,我們可以通過手機(jī)號前3位的網(wǎng)絡(luò)識別號辨別手機(jī)號所屬運(yùn)營商。在我國手機(jī)號運(yùn)營商有移動、聯(lián)通、電信,各大運(yùn)營商的網(wǎng)絡(luò)識別號如表1所示。

1 運(yùn)營商和網(wǎng)絡(luò)識別號

運(yùn)營商

號碼段

移動

134135136137138139147148150151152157158 159165178182183184187188198

聯(lián)通

130131132140145146155156166185186175176

電信

133149153180181189177173174191199

本實(shí)例要求編寫程序,實(shí)現(xiàn)判斷輸入的手機(jī)號碼是否合法以及判斷其所屬的運(yùn)營商的功能。

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(" 中國聯(lián)通") else:print("中國電信 ")

總結(jié)

以上是生活随笔為你收集整理的python练习题(3)--字符串及正则表达式的应用的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。