How would you check if each word in a string begins with a capital letter?
CodePudding user response:
You can use isupper()
applied to the first character of each word (which are obtained from split
), and then apply all
:
s = 'How would you check if each word in a string begins with a capital letter?'
t = 'Hello, World!'
def all_startswith_cap(s):
return all(w[0].isupper() for w in s.split())
print(all_startswith_cap(s)) # False
print(all_startswith_cap(t)) # True
CodePudding user response:
sentense = "the quick Brown fox Jumps over the lazy Dog"
words = sentense.split(' ')
for word in words:
if word[0].isupper():
print('{0} contains first capital letter'.format(word))
CodePudding user response:
Check that no word begins with a lower case letter?
not re.search(r'\b[a-z]', s)
CodePudding user response:
s = "Stackoverflow"
s[0].isupper()