输入一个姓名,判断是否姓王
startswith() 方法用于检查字符串是否是以指定子字符串开头,如果是则返回 True,否则返回 False。如果参数 beg 和 end 指定值,则在指定范围内检查。 startswith()方法语法:str.startswith(str, beg=0,end=len(string));
b = input("输入姓名:")
# 判断字符串以xx开始
if str(b).startswith("王"):
print("姓王")
else:
print("不姓王")
b = input("输入姓名:")
endswith() 方法用于判断字符串是否以指定后缀结尾,如果以指定后缀结尾返回True,否则返回False。可选参数"start"与"end"为检索字符串的开始与结束位置。 endswith()方法语法:str.endswith(suffix[, start[, end]])
b = input("输入姓名:")
# 判断字符串以xx结尾
if str(b).endswith("三"):
print("姓名以三结尾")
else:
print("姓名不以三结尾")