How to search for an uppercase letter inside a string and return a list of words with and without capital letters

My homework is to write a program that reads a line from the user and creates a list of words from the input. Create two lists: one contains words containing at least one uppercase letter and one of the words that does not contain any uppercase letters. Use a single loop to print words with uppercase letters in them, followed by words without uppercase letters in them, one word per line.

What I know is wrong:

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 purpose, the character may appear anywhere inside the word. For example, 'tHis is a sTring' .

I played with a solution that looked like this, just to see if I could get words with CAPS. But this just doesn't work:

  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 regex would probably do a better job than spelling the entire alphabet.

Any help is greatly appreciated. Needless to say, I'm new to python.

+7
source share
9 answers

Hint: "Create two lists"

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

The way to use for .. else in is wrong - the else block is executed when there is no break in the loop. The logic you are trying to do is as follows:

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

which you can also write much shorter than any

 if any(c in "ABCDEFGHIJKLMNOPQRSTUVWXYZ" for c in s): # <do something> else: # <do something> 
+4
source
 >>> w = 'AbcDefgHijkL' >>> r = re.findall('([AZ])', word) >>> r ['A', 'D', 'H', 'L'] 

It can give you all the letters in caps in one word ... Just share the idea

 >>> r = re.findall('([AZ][az]+)', w) >>> r ['Abc', 'Defg', 'Hijk'] 

All the words starting with the letter Caps will be given above. Note: the latter is not recorded, since it does not make a word, but even this can be fixed

 >>> r = re.findall('([AZ][az]*)', w) >>> r ['Abc', 'Defg', 'Hijk', 'L'] 

This will return true if the capital letter is in the word:

 >>> word = 'abcdD' >>> bool(re.search('([AZ])', word)) 
+3
source

It appears that regular expressions will be easier for the first part of the problem (a regular expression that just searches for [AZ] should do the trick).

In the second part, I would recommend using two lists, since this is an easy way to print everything in one cycle. Have one list of non_upper_words and one of upper_words.

So, the main circuit of the program will be:

  • divide the string into an array of words.
  • for each word in the array: if regex returns true, add in upper_words. Else: add to not_upper_words.
  • print each word in the first array, and then in the second.

I wrote this in pseudo code because it is a programming purpose, so you really have to write the code yourself. Hope this helps!

+2
source

You can use the isperper 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 the islower method as follows:

 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 the istitle method:

 titled = [] lowers = [] for word in text.split(): titled.append(word) if word.istitle() else lower.append(word) 

And with a simple if else statement:

 titled = [] lowers = [] for word in text.split(): if word.istitle(): titled.append(word) else: lower.append(word) 
+1
source

You can use List Consrehensions to get all uppercase and lowercase 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 Output:

List Cap ['H', 'T', 'P'] and List Lower ['i', 'h', 'i', 's', 'i', 's', 'd', 'e' , 'M', 'o', 'y', 't', 'h', 'o', 'n', 'p', 'r', 'o', 'g', 'r' , 'a', 'm']

The number of caps 3 and the number of lower 22

+1
source

Try the following:

  • Separate the line in the list where each item is a separate word.
  • For each word in this list, iterates and checks for capital letters (consider string constants such as string.uppercase ). If it has an uppercase letter, insert it at the top of the list of results. If not, add it to the end of the list of results.
  • Iterate the results, print them. Or, if you want to avoid iteration, append the items in the string using the newline \n .
0
source

Thanks to everyone for your contribution and help, all this was very informative. Here is the answer that I finally presented.

 s = input("Please enter a sentence: ") withcap = [] without = [] for word in s.split(): if word.islower(): without.append(word) else: withcap.append(word) onelist = withcap + without for word in onelist: print (word) 
0
source

I think your answer can only be a word search where the first letter is capitalized. To find words that contain a capital letter anywhere in the word, you will need to list each letter in the word, for example:

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

My regex is:

 vendor = "MyNameIsJoe. IWorkInDDFinc." ven = re.split(r'(?<=[az])[AZ]|[AZ](?=[az])', vendor) 

I need to split the word that would happen: My Name Is Joe. I Work In DDF inc. My Name Is Joe. I Work In DDF inc.

0
source

All Articles