【语言处理与Python】1.4回到Python:决策与控制

时间:2022-09-08 14:08:53

【条件】
-常用句型 [w for w in text if condition]
-常用的比较运算符
s.startswith(t)测试s是否以t开头
s.endswith(t)测试s是否以t结尾
t in s测试s是否包含t
s.islower()测试s中所有字符是否都是小写字母
s.isupper()测试s中所有字符是否都是大写字母
s.isalpha()测试s中所有字符是否都是字母
s.isalnum()测试s中所有字符是否都是字母或者数字
s.isdigit()测试s中所有字符是否都是数字
s.istitle()测试s中是否首字母大写
【条件结构】
for token in sent1:
    if token.islower():
        print token,'is a lowercase word'
    elif token.istitle():
        print token,'is a titlecase word'
    else:
        print token,'is punctuation'
注意:“,”代表是一行输出