Python程序设计习题答案-(5)详解与示范.docx
第五章课后习题1.在字符串操作时,可以利用连接操作符或字符串对象方法将多个字符串合并为一个。 【参考答案】 + join()方法。2.以下是生成随机6位密码的程序示例:【参考答案】 import random s = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890!@#$%^&*" pwd = ''.join((random.choice(s) for i in range(6))) print(pwd) 3.可使用如下方法将"A bad workman quarrels with his Tools."中的T替换为t: 方法一: s = 'A bad workman quarrels with his Tools.' s = s.replace('T','t') print(s) 方法二: import re s = 'A bad workman quarrels with his Tools.' result = re.sub(r'\bT', 't', s) print(result)
用户评论