更新时间:2022-10-29 16:44:02
>>>w = 'AbcDefgHijkL'>>>r = re.findall('([A-Z])', word)>>>r['A', 'D', 'H', 'L']
这可以让你在一个单词中用大写字母...只是分享想法
>>>r = re.findall('([A-Z][a-z]+)', w)>>>r['ABC'、'Defg'、'Hijk']以上将为您提供所有以大写字母开头的单词.注意:最后一个没有被捕获,因为它不会产生一个词,但即使是可以捕获的
>>>r = re.findall('([A-Z][a-z]*)', w)>>>r['ABC'、'Defg'、'Hijk'、'L']如果在单词中找到大写字母,这将返回真:
>>>字 = 'abcdD'>>>bool(re.search('([A-Z])', word))My homework assignment is to Write a program that reads a string from the user and creates a list of words from the input.Create two lists, one containing the words that contain at least one upper-case letter and one of the words that don't contain any upper-case letters. Use a single for loop to print out the words with upper-case letters in them, followed by the words with no upper-case letters in them, one word per line.
What I know is not correct:
s= input("Enter your string: ")
words = sorted(s.strip().split())
for word in words:
print (word)
Because it only sorts the sequence if the Capitol is in the first character. For this assignment a character could appear any where within a word. Such as, 'tHis is a sTring'
.
I was playing around with a solution that looked similar to this, just to see if I could get the words with CAPS out..But it just wasnt working:
s = input("Please enter a sentence: ")
while True:
cap = 0
s = s.strip().split()
for c in s:
if c in "ABCDEFGHIJKLMNOPQRSTUVWXYZ":
print(c[:cap])
cap += 1
else:
print("not the answer")
break
But a regular expression would probably do a better job than writing out the whole alphabet.
Any help is much appreciated. Needless to say I am new to python.
>>> w = 'AbcDefgHijkL'
>>> r = re.findall('([A-Z])', word)
>>> r
['A', 'D', 'H', 'L']
This can give you all letters in caps in a word...Just sharing the idea
>>> r = re.findall('([A-Z][a-z]+)', w)
>>> r
['Abc', 'Defg', 'Hijk']
Above will give you all words starting with Caps letter. Note: Last one not captured as it does not make a word but even that can be captured
>>> r = re.findall('([A-Z][a-z]*)', w)
>>> r
['Abc', 'Defg', 'Hijk', 'L']
This will return true if capital letter is found in the word:
>>> word = 'abcdD'
>>> bool(re.search('([A-Z])', word))