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 con
>>> 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))
Sounds like regexs would be easier for the first part of the problem (a regex that just looks for [A-Z] should do the trick).
For the second part, I'd recommend using two lists, as that's an easy way to print everything out in one for loop. Have one list of non_upper_words and one of upper_words.
So, the basic outline of the program would be:
I wrote this out in pseudo-code because it's a programming assignment, so you should really write the actual code yourself. Hope it helps!
You can use List Comprehensions to get all upper case characters and lower case characters.
def get_all_cap_lowercase_list(inputStr):
cap_temp_list = [c for c in inputStr if c.isupper()]
low_temp_list = [c for c in inputStr if c.islower()]
print("List of Cap {0} and List of Lower {1}".format(cap_temp_list,low_temp_list))
upper_case_count = len(cap_temp_list)
lower_case_count = len(low_temp_list)
print("Count of Cap {0} and Count of Lower {1}".format(upper_case_count,lower_case_count))
get_all_cap_lowercase_list("Hi This is demo Python program")
And The output is:
List of Cap ['H', 'T', 'P'] and List of Lower ['i', 'h', 'i', 's', 'i', 's', 'd', 'e', 'm', 'o', 'y', 't', 'h', 'o', 'n', 'p', 'r', 'o', 'g', 'r', 'a', 'm']
Count of Cap 3 and Count of Lower 22
You can use isupper method for your purpose:
text = 'sssample Text with And without'
uppers = []
lowers = []
# Note that loop below could be modified to skip ,.-\/; and etc if neccessary
for word in text.split():
uppers.append(word) if word[0].isupper() else lowers.append(word)
EDITED: You can also use islower method the following way:
text = 'sssample Text with And without'
other = []
lowers = []
# Note that loop below could be modified to skip ,.-\/; and etc if neccessary
for word in text.split():
lowers.append(word) if word.islower() else other.append(word)
OR depends on what you really need you can take a look at istitle method:
titled = []
lowers = []
for word in text.split():
titled.append(word) if word.istitle() else lower.append(word)
AND with simple if else statement:
titled = []
lowers = []
for word in text.split():
if word.istitle():
titled.append(word)
else:
lower.append(word)
I think your answer might only be searching for words where the first letter is capitalized. To find words that contain a capital letter anywhere in the word, you'd have to enumerate over each letter in the word, like this:
uin = input("Enter your text: ")
##create lists to hold upper and lower case words
up_words = []
no_up_words = []
for i, word in enumerate(uin.strip().split()):
if word.islower():
no_up_words.append(word)
else:
up_words.append(word)
print(up_words, no_up_words)
Try doing the following:
\n.