删除单词以大写开头

时间:2015-03-18 22:13:20

标签: python-2.7 startswith

所以我是python的新手,我想过滤掉一个以大写字母开头的文本中的所有单词,所以我对python的知识有限,我这样做了:

def filterupper(text):
    upper = string.ascii_uppercase
    filteredupper = [w for w in text not in startswith(upper)]
return filteredupper

并且出现了这个错误

 File "<pyshell#58>", line 3, in filterupper
filteredupper = [w for w in text not in startswith(upper)]

NameError:未定义全局名称“startswith”

所以我试过这个:

def filterupper(text):
    upper = string.ascii_uppercase
    filteredupper = [w for w in text not in upper]
return filteredupper 

并且出现了这个错误:

File "<pyshell#55>", line 3, in filterupper
filteredupper = [w for w in text not in upper]
TypeError: 'in <string>' requires string as left operand, not list

所以任何人都可以告诉我如何删除单词以大写字母开头,并告诉我在这些代码中我做错了什么

谢谢

1 个答案:

答案 0 :(得分:0)

使用str.islower()尝试以下方法检查字母是否为小写:

def filterupper(text):
    return " ".join([word for word in text.split() if word[0].islower()])

>>> filterupper("My name is Bob And I am Cool")
"name is am"
>>>