Python第五周

 

 一.    实验目的和要求

掌握python字符串拼接、截取、分割和合并等操作。

二.   实验环境

 python 3.10 64-bit

三.   实验过程

实例1

代码如下:

1 programmer_1 ='程序员甲:搞IT太辛苦了,我想换行......怎么办?'
2 programmer_2 ='程序员乙:敲一下回车键'
3 print(programmer_1 + '\n' + programmer_2)

运行结果:

实例2

代码如下:

1 programer_1 = '你知道我的生日吗?'
2 print('程序员甲说:',programer_1)
3 programer_2 = '输入你的身份证号码。'
4 print('程序员乙说:',programer_2)
5 idcard = '123456199006277890'
6 print('程序员甲说:',idcard)
7 birthday = idcard[6:10] + '' + idcard[10:12] + '' + idcard[12:14] + ''
8 print('程序员乙说:','你是' + birthday + '出生的,所以你的生日是' + birthday[5:])

运行结果:

实例3

代码如下:

1 str1 = '@明日科技 @扎克伯格 @俞敏洪'
2 list1 = str1.split(' ')
3 print('您@的好友有:')
4 for item in list1:
5     print(item[1:])

运行结果:

实例4

代码如下:

1 list_friend = ['明日科技','扎克伯格','俞敏洪','马云','马化腾']
2 str_friend = '@'.join(list_friend)
3 at = '@'+str_friend
4 print('您要@的好友:',at)

运行结果:

实例5

代码如下:

1 username_1 = '|MingRi|mr|mingrisoft|WGH|MRSOFT|'
2 username_2 =username_1.lower()
3 regname_1 = input('输入要注册的会员名称:')
4 regname_2 = '|' + regname_1.lower() +'|'
5 if regname_2 in username_2:
6     print('会员名',regname_1,'已经存在!')
7 else:
8     print('会员名',regname_1,'可以注册!')

运行结果:

实例6

代码如下:

1 import math
2 print('1251+3950的结果是(以货币形式显示):¥{:,.2f}元'.format(1251+3950))
3 print('{0:.1f}用科学计数法表示:{0:E}'.format(120000.1))
4 print('Π取5位小数:{:.5f}'.format(math.pi))
5 print('{0:d}的16进制结果是:{0:#x}'.format(100))
6 print('天才是由{:.0%}的灵感,加上{:.0%}的汗水。'.format(0.01,0.99))

运行结果:

实例7

代码如下:

 1 import re
 2 pattern = r'(13[4-9]\d{8})$|(15[01289]\d{8})$'
 3 mobile = '13634222222'
 4 match = re.match(pattern,mobile)
 5 if match == None:
 6     print(mobile,'不是有效的中国移动手机号码。')
 7 else:
 8     print(mobile,'是有效的中国移动手机号码。')
 9 mobile = '13144222221'
10 match = re.match(pattern,mobile)
11 if match == None:
12     print(mobile,'不是有效的中国移动手机号码。')
13 else:
14     print(mobile,'是有效的中国移动手机号码。')

运行结果:

实例8

代码如下:

 1 import re
 2 pattern = r'(黑客)|(抓包)|(监听)|(Trojan)'
 3 about = '我是程序员,我喜欢看黑客方面的图书,想研究一下Trojan。'
 4 match = re.search(pattern,about)
 5 if match == None:
 6     print(about,'@ 安全!')
 7 else:
 8     print(about,'@ 出现危险词汇!')
 9 about = '我是一名程序员,我喜欢看计算机网络方面的图书,喜欢开发网站。'
10 match = re.match(pattern,about)
11 if match == None:
12     print(about,'@ 安全!')
13 else:
14     print(about,'@ 出现了危险词汇!')

运行结果:

实例9

代码如下:

1 import re
2 pattern = r'(黑客)|(抓包)|(监听)|(Trojan)'
3 about = '我是一名程序员,我喜欢看黑客方面的图书,想研究一下Trojan。\n'
4 sub = re.sub(pattern,'@_@',about)
5 print(sub)
6 about = '我是一名程序员,我喜欢看计算机网络方面的图书,喜欢开发网站。'
7 sub = re.sub(pattern,'@_@',about)
8 print(sub)

运行结果:

实例10

代码如下:

1 import re
2 str1='@明日科技@扎克伯格@俞敏洪'
3 pattern = r'\s*@'
4 list1 = re.split(pattern,str1)
5 print('您@的好友有:')
6 for item in list1:
7     if item !="":
8         print(item)

运行结果:

实战1:

代码如下:

1 print("象棋口诀:")
2 str1 = '马走日 ,'
3 str2 = '象走田 ,'
4 str3 = '车走直路炮翻山 ,'
5 str4 = '士走斜线护将边 ,'
6 str5 = '小卒一去不回还 。'
7 print(str1 + '\n' + str2 + '\n' + str3 + '\n' + str4 + '\n' + str5)

 

 运行结果:

 实战2

代码如下:

1 str1 = '津A·12345','沪A·23456','京A·34567'
2 for i in range(len(str1)):
3     print('' + str(i + 1) + '张车牌号码:\n' + str1[i])
4     if str1[i][0] == '':       #判断第i个元素的第0个字段是否符合条件
5         print("这张号牌的归属地:天津")
6     elif str1[i][0] == '':
7         print("这张号牌的归属地:上海")
8     elif str1[i][0] == '':
9         print("这张号牌的归属地:北京")

运行结果:

 

 实战3

代码如下:

 1 import random
 2 import decimal
 3 print("--------------模拟微信抢红包-------------")
 4 money = float(input("请输入要装入红包的总金额(元):"))
 5 count = int(input("请输入红包的个数(个):"))
 6 for num in range(1, count + 1):         
 7     if num == count:
 8         end = money                       
 9     else:
10         end = random.uniform(0.01, money) 
11         end = round(end, 2)               
12         money = money - end
13         money = round(money, 2)           
14     print("" + str(count) + "个红包:" + str(end) + "")

 

 

 运行结果:

 

 

 实战4:

代码如下:

1 weather='2018年4月17日 \t 天气预报:{:s} \t 20C~7°℃ \t 微风转西风3~4级\n \
2 08:00 \t 天气预报:{:s} \t 13℃ \t 微风\n\
3  12:00 \t 天气预报:{:s} \t 19℃ \t 微风\n\
4  16:00 \t 天气预报:{:s} \t 18℃ \t 西风3~4级\n\
5  20:00 \t 天气预报:{:s} \t 15℃ \t 西风3~4级\n\
6  00:00 \t 天气预报:{:s} \t 12℃ \t 微风\n\
7  04:00 \t 天气预报:{:s} \t 9℃  \t 微风'
8 ans = weather.format('', '', '', '', '', '', '')
9 print(ans)

运行结果:

 

 

 实践1:

代码如下:

1 import re
2 pattern = r"^(?![A-Za-z0-9]+$)(?![a-z0-9\\W]+$)(?![A-Za-z\\W]+$)(?![A-Z0-9\\W]+$)^.{8}$"
3 string = ["uei652t6t#%^^&&#4", "66666666", "!sb45441"]
4 for item in string:
5     res = re.search(pattern, item)
6     if res:
7         print(item, "匹配成功")
8     else:
9         print(item, "匹配失败,不是由数字、字母、特殊字符组成的8位密码")

 

运行结果:

 

 

 实践2:

代码如下:

 1 import re
 2 pattern = r"(^44\d{13}$)|(^44\d{16}$)|(^44\d{15})(\d|X|x)$"
 3 id = ["444444444444444444","44444444444X","404444444444444",]
 4 for item in id:
 5     if (len(item) == 15) or (len(item) == 18):
 6         search = re.search(pattern, item)    
 7         if search == None:     
 8             print(item + "不是广东省的身份证号码,头两位数字为:" + item[0:2])
 9         else:                  
10             print(item + "是广东省的身份证号码,头两位数字为:" + item[0:2])
11     else:         
12             print(item + "不符合身份证号码格式")

 

运行结果:

 

posted on 2022-10-11 19:36  声声声  阅读(53)  评论(0)    收藏  举报

导航