24.输入用户信息:
提示用户输入姓名,对输入的内容进行判断,如果输入中包含数字或特殊符号,提示用户输入不正确,请重新输入。
如果输入正确,提示用户"您输入的用户名为:xxx"
提示用户输入年龄,如果输入是 0–100 岁之间的数字,提示用户,您输入的年龄为XX 岁,否则提示用户,您输入的有误,请重新输入
name = input("请输入姓名:")
age = int(input("请输入年龄:"))
while True:
if name.isalpha() and 0 < age < 100:
print("您输入的用户为:%s" % name)
print("您输入的年龄为%d岁" % age)
break
else:
print("您输入不正确,请重新输入")
print("您输入不正确,请重新输入")
name = input("请输入姓名:")
age = int(input("请输入年龄:"))
25.字符串处理:
现有字符串 msg = “hello python ni hao” 将其中所有的空格改为下划线,得到新字符串:“hello_python_ni_hao”
现有字符串 msg = “hello python ni hao” 统计字符串"o" 出现的次数和位置
现有字符串 msg = “hello python ni hao” 删除字符串中所有的空格,并打印结果:“hellopythonnihao”
msg = "hello python ni hao"
mgs = msg.replace(" ", "_", 3)
print(mgs)
msg = "hello python ni hao"
m = len(msg)
g = msg.count("o")
print(g)
idx = msg.find("o", 0, 17)
idxx = msg.find("o", 6, 17)
idxxx= msg.find("o", 13, 17)
print(idx)
print(idxx)
print(idxxx)
msg = "hello python ni hao"
mgs = msg.replace(" ", "")
print(mgs)