1. 程式人生 > 程式設計 >Python字串與正則表示式詳細介紹

Python字串與正則表示式詳細介紹

目錄
  • 一、字串相關操作
  • 二、正則表示式相關操作

一、字串相關操作

1.統計所輸入字串中單詞的個數,單詞之間用空格分隔。其執行效果如下圖所示。

s=input('請輸入字串:')
sum=1
for i in s:
  if i==' ':
    sum+=1
print('方法一:',end='')
print('其中的單詞總數有:',sum)

list=s.split(' ')
print('方法二:',len(list))

2. 編寫程式,給出一個字串,將其中的字元“E”用空格替換後輸出。

a=input('請輸入一個字串:')
print('替換前:',a)
a=a.replace('E',' ')
print('替換後:',a)

3. 從鍵盤互動式輸人一個人的 18 位的身份證號,以類似於“2001 年 09 月 12 日”的形式輸出該人的出生日期。

idc=input("請輸入身份證號:")
print(str.format('出生日期:{0}年{1}月{2}日',idc[6:10],idc[10:12http://www.cppcns.com],idc[12:14]))

4.將字串'abcdefg'使用函式的方式進行倒序輸出

list='abcdefg'
print(list[::-1])

5. 在我們的生活中,節假日的問候是必不可少的,請使用字串格式化的方式寫OwSay一個新年問候語模板.

name=input("請輸入姓名:")
print("祝{}新年快樂!".format(name))

6. 使用者輸入一個字串,將下標為偶數的字元提出來合併成一個新的字串 A,再將下標為奇數的字元提出來合併成一個新的字串 B,再將字串 A 和 B 連線起來並輸出。

s=input('請輸入字串:')
A=s[0::2]
B=s[1::2]
print('A=',A)
print('B=',B)
print(A+B)

7. 請根據下列需求,編寫一個程式。使用者輸入一個字串,請將字串中的所有字母全部向後移動一位,最後一個字母放到字元開頭,最後將新的字串輸出。

s=input('請輸入字串:')
s_new=s[-1]+s[:len(s)-1] #s[-1]表示s最後一位,s[:len(s)-1]表示切片到倒數第二位
print(s_new)

8. 基於 input 函式,對輸入的字串進行處理,並將返回替換了某些字元的字串,規則如下:

  • 如果一個字母是大寫子音,請將該字元替換為“Iron”。
  • 如果字母是小寫子音或非字母字元,則對該字元不執行任何操作。
  • 如果一個字母是大寫母音,請將該字元替換為“Iron Yard”。
  • 如果一個字母是小寫母音,請用“Yard”替換該字元。
import re
text=input("請輸入字串:")
for i in text:
  if i=='A' orhttp://www.cppcns.com i=='O' or i=='E' or i=='I' or i=='U':
    a=re.sub('[AOEIU]','Iron Yard',text)
  if i == 'a' or i == 'o' or i == 'e' or i == 'i' or i == 'u':
    a=re.sub('[aoeiu]','Yard',text)
  if i > 'A' and i < 'Z':
    a=re.sub('[A-Z-[AOEIU]]','Iron',text)
print("替換後的字元為:",a)

二、正則表示式相關操作

1. 寫出能夠匹配163 郵箱(@163.com)的正則表示式,並用 re.match 方法和郵箱 sda123(wer)[email protected] 作為測試驗證。

import re
s=input("請輸入郵箱:")
if re.match(r'.*[email protected]',s):
  print('是')
else:
  print('不是')

2. 利用 re 庫中的 search、findall 或 search 函式從以下三個字串中提取出所有的漢字,輸出的結果分別為“大連理工大學”,“重慶大學”以及“中南財經大學” 。(提示:字串 st2,str3 中有空格)。

  • str1="""<td width="160">大連理工大學</td>"""
  • str2="""<td width="160"><a href="../news/list_117.html"class="keyWord" target="_blank">重慶</a>大學</tOwSayd>"""
  • str3="""<td width="160"> 中南 <a href="../news/list_197.html"class="keyWord" target="_blank"> 財經 </a><ahref="../news/list_201.html" class="keyWord" target="_blank">政法</a>大學</td>"""
import re
str1="""<td width="160">大連理工大學</td>"""
str2="""<td width="160"><a href="../news/list_117.html" rel="external nofollow"  rel="external nofollow"  class="keyWord" target="_blank">重慶</a>大學</td>"""
str3="""<td width="160">中南<a href="../news/list_197.html" rel="external nofollow"  rel="external nofollow"  class="keyWord" target="_blank">財經</a><a href="../news/list_201.html" rel="external nofollow"  rel="external nofollow"  class="keyWord" target="_blank">政法</a>大學</td>"""
re1=re.search("""<td width="160">(.*?)</td>""",str1).group(1)
print(''.join(map(str,re1)))
re2=re.search("""<td width="160"><a href="../news/list_117.html" rel="external nofollow"  rel="external nofollow"  class="keyWord" target="_blank">(.*?)</a>(.*?)</td>""",str2).group(1,2)
print(''.join(map(str,re2)))
re3=re.search("""<td width="160">(.*?)<a href="../news/list_197.html" rel="external nofollow"  rel="external nofollow"  class="keyWord" target="_blank">(.*?)</a><a href="../news/list_201.html" rel="external nofollow"  rel="external nofollow"  class="keyWord" target="_blank">(.*?)</a>(.*?)</td>""",str3).group(1,2,3,4)
print(''.join(map(str,re3)))

到此這篇關於字串與正則表示式詳細介紹的文章就介紹到這了,更多相關Python字串與正則表示式內容請搜尋我們以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援我們!