Skip to content Skip to sidebar Skip to footer

How To Search For A Capital Letter Within A String And Return The List Of Words With And Without Capital Letters

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

Solution 1:

>>> 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))

Solution 2:

Hint: "Create two lists"

s= input("Enter your string: ")
withcap = []
without = []
for word in s.strip().split():
    # your turn

The way you are using the for .. else in is wrong - the else block is executed when there is no break from the loop. The logic you are trying to do looks like this

forcin s:if c.isupper():# s contains a capital letter# <do something>break# one such letter is enoughelse:# we did't `break` out of the loop# therefore have no capital letter in s# <do something>

which you can also write much shorter with any

ifany(cin"ABCDEFGHIJKLMNOPQRSTUVWXYZ"forcin s):# <do something>else:# <do something>

Solution 3:

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:

  1. split the string into an array of words.
  2. for each word in array: if regex returns true, add to upper_words. Else: add to non_upper_words.
  3. print each word in the first array and then in the second.

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!

Solution 4:

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)

Solution 5:

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

Post a Comment for "How To Search For A Capital Letter Within A String And Return The List Of Words With And Without Capital Letters"