Python程序设计之字符操作(字符串)

1.字符串检验密码强度密码由六位数组成
设置四种密码强弱形式:
a很弱:只包含数字、小写字母、大写字母、和标点符号中一种;
b一般:包含上述任意两种字符
c较强:包含上述任意三种字符
d很强:包含上述四种字符

import string
def check(pwd):
#密码必须至少包含6个字符
	if len(pwd)<6:
    		return 'weak'
    	d={
   1:'week',2:'blew middle',3:'above middle',4:'strong'}	#使用字典设置强弱对应类型
    	r=[False]*4	#需要判断用户输入字符中包含字符的种类,通过列表来判断
    	for ch in pwd:	#使用for循环遍历pwd字符串
		if not r[0] and ch in string.digits:	#遍历是否存在数字
			r[0]=True
		if not r[1] and ch in string.ascii_lowercase:	#遍历是否存在小写字母
			r[1]=True
		if not r[2] and ch in string.ascii_uppercase:	#遍历是否存在大写字母
			r[2]=True
		if not r[3] and ch in ',.|;;?<>':	#遍历是否存在指定字符
			r[3]=True
	return d.get(r.cout(True),'error')	#记录r中True的数量,并返回字典中对应的值
		
n=str(input('请输入你的密码(六位字符):'))					    			
print(check(n))	

代码链接:
https://download.csdn.net/download/qxyloveyy/12184552

2.对文件中字符串的操作
实现以下功能:
a.使用open()函数打开并读取文件中的每一行
b.重新生成一个文件,文件名是在原文件名上添加一个后缀的形式,例如原名Car.py=>Car_news.py
c.在新文件中进行写的操作,通过enumerate()遍历行数,根据字符串比较输出指定内容,例如通过strip()和startwith()函数可以实现对开头字符的判断
d.最后使用close()函数关闭文件

import sys
import re

def checkFormats(lines, desFileName):
    fp = open(desFileName, 'w')
    for i, line in enumerate(lines):
        print('=' * 30)
        print('Line:', i + 1)
        if line.strip().startswith('#'):
            print(' ' * 10 + 'Comments.Pass.')
            fp.write(line)
            continue
        flag = True
        # check operator symbols
        symbols = [',', '+', '-', '*', '/', '//', '**', '>>', '<<', '+=', '-=', '*=', '/=']
        temp_line = line
        for symbol in symbols:
            pattern = re.compile(r'\s*' + re.escape(symbol) + r'\s*')
            temp_line = pattern.split(temp_line)
            sep = ' ' + symbol + ' '
            temp_line = sep.join(temp_line)
        if line != temp_line:
            flag = False
            print(' ' * 10 + 'You may miss some blank spaces in this line.')
            # check import statement
        if line.strip().startswith('import'):
            if ',' in line:
                flag = False
                print(' ' * 10 + "You'd better import one module at a time.")
                temp_line = line.strip()
                modules = temp_line[temp_line.index(' ') + 1:]
               
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值