Python字符串习题,,请用代码实现: a.


请用代码实现:
a. 利用下划线将列表的每一个元素拼接成字符串,li = "alexericrain"

test="alexericrain"v=‘_‘.join(test)print(v)

运行结果:

a_l_e_x_e_r_i_c_r_a_i_n


b. 利用下划线将列表的每一个元素拼接成字符串,li = [‘alex‘,‘eric‘,‘rain‘] (可选)

test=[‘alex‘,‘eric‘,‘rain‘]v=‘_‘.join([‘alex‘,‘eric‘,‘rain‘])print(v)

运行结果:

alex_eric_rain

实现一个整数加法计算器:
如:
content=input(‘请输入内容:‘) # 如: 5+9 或 5+9 或 5+9

test=input("请输入\n")v1,v2=test.split(‘+‘)v1=int(v1)v2=int(v2)v3=v1+v2print(v3)

统计计算用户输入的内容中有几个十进制小数?几个字母?
如:content=input(‘请输入内容:‘) # 如:asduiaf878123jkjsfd-213928

答案:

content=input("请输入内容\n")i=0j=0for name in content:    tp=name.isalpha()    if tp ==False:        i+=1    else:        j+=1print("number is ",i)print("string is",j)

制作趣味模板程序
需求:等待用户输入名字、地点、爱好,根据用户的名字和爱好进行任意现实

name=input("请输入名字\n")address=input("请输入地址\n")like=input("请输入爱好\n")test="{0}喜欢在{1}{2}"new_test=test.format(name,address,like)print(new_test)

制作随机验证码,不区分大小写。
流程:
- 用户执行程序
- 给用户显示需要输入的验证码
- 用户输入的值
用户输入的值和显示的值相同时现实正确信息;否则继续生成随机验证码继续等待用户输入
生成随机验证码代码示例:

def check_code():    import random    checkcode=‘‘    for i in range(4):        current=random.randrange(0,4)        if current !=i:            temp=chr(random.randrange(65,90))        else:            temp=random.randrange(0,9)        checkcode+=str(temp)    return checkcodecode=check_code()print(code)while True:    code=check_code()    print(code)    name=input("please input\n")    if name != code:        print("input error")    else:        print("input success")        break

运行结果:

XN2R
please input
NG
input error
IGDP
please input
IGDP
input success

开发敏感词语过滤程序,提示用户输入内容,如果用户输入的内容中包含特殊的字符:如 "苍老师" "东京热",则将内容替换为 ***

m1=str.maketrans("苍老师","***")m2=str.maketrans("东京热","***")search=input("请输入你要搜索的内容\n")if search =="苍老师":    new_search=search.translate(m1)    print(new_search)elif search =="东京热":    new_search = search.translate(m2)    print(new_search)else:    print("你搜索的内容为",search)

制作表格
循环提示用户输入:用户名、密码、邮箱 (要求用户输入的长度不超过 20 个字符,如果超过则只有前 20 个字符有效)
如果用户输入 q 或 Q 表示不再继续输入,将用户输入的内容以表格形式打印

while True:    name = input("please input your name\n")    if name =="q" or name =="Q":        print("正在退出")        break    else:        print("请继续输入")    passwd = input("please input your password\n")    email = input("please input your e-mail\n")    new_name = name.expandtabs(20)    new_passwd = passwd.expandtabs(20)    new_email = email.expandtabs(20)print(new_name,new_passwd,new_email)

Python字符串习题

评论关闭