知识点
strip()
语法:str.strip([chars])
参数chars:移除字符串头尾指定的字符序列
例如:
str.strip(‘qinjian’); # 去除首尾字符qinjian
split()
语法:str.split(str=””, num=string.count(str))
参数str:分隔符,默认为所有的空字符,包括空格、换行(n)、制表符(t)等。
参数num:分割次数。默认为 -1, 即分隔所有。
例如:
tmp_str = ‘2020-12-01’
tmp_str.split(‘-‘) # 以“-”符号分割tmp_str字符串,得到如下列表
[‘2020′, ’12’, ’01’]
实例
用户按照格式输入自己的生日,程序判断对应的星座以及星座符号# 星座判断列表
sdate = [20, 19, 21, 20, 21, 22, 23, 23, 23, 24, 23, 22]
conts = [‘魔羯座’, ‘水瓶座’, ‘双鱼座’, ‘白羊座’, ‘金牛座’, ‘双子座’, ‘巨蟹座’, ‘狮子座’, ‘处女座’, ‘天秤座’, ‘天蝎座’, ‘射手座’, ‘魔羯座7’]
sings = [‘♑’, ‘♒’, ‘♓’, ‘♈’, ‘♉’, ‘♊’, ‘♋’, ‘♌’, ‘♍’, ‘♎’, ‘♏’, ‘♐’, ‘♑7’]
# 输入生日
# strip(‘ ’)去除首尾空格
birth = input(‘请输入您的出生年月,格式为:2001-02-21’).strip(‘ ‘)
# 分割年月日到列表
cbir = birth.split(‘-‘)
print(cbir)
# 提取月数据
cmonth = str(cbir[1])
# 提取日数据
cdate = str(cbir[2])
# 判断星座方法
def sign(month, date):
print(month, date)
# 所有列表是从0开始,所以有减1操作
if int(date) < sdate[int(month) – 1]:
print(conts[int(month)-1])
print(sings[int(month)-1])
else:
print(conts[int(month)])
print(sings[int(month)])
if __name__ == ‘__main__’:
sign(cmonth, cdate)