Need to change capitalize python to interpret 3 different types of inputs the SAME with the raw_input function

StackOverflow https://stackoverflow.com/questions/17897802

Question

Wether I enter 1 word, 1 word and 1 word in parenthesis, and 2 words. ie. "American" or "american (grill)" or "american italian" only the first letter in each word will be capitalized.

I have wrote my code

wcity = raw_input("Please enter a city ==> ")
print
wtype = raw_input("Please enter a restaurant type ==> ")
print

And changed it to:

wcity = raw_input("Please enter a city ==> ").capitalize()
print
wtype = raw_input("Please enter a restaurant type ==> ").capitalize()
print

I need to change .capitalize for wtype it so when I input for wtype EITHER (not all, only 1 of them at a time)

aMERICan
aMerican iTalian 
american (nEw) 

Python interprets it as

American
American Italian    
American (New)
  • BUT I need python to read it the same wether its 1 word "aMERICan" or "American (New)"

any ideas, ive fooled around with upper and lower and those dont work better than the .capitalize

Any ideas?

I can do .capitalize as I do in wcity because my cities don't have parenthesis with 1 word inside them or 2 words

My inputs for wcity are not:

  • chicago (troy)

  • vegas (reno)

  • chicago bronx (2 words)

My inputs for wcity are:

  • chicago
Was it helpful?

Solution

You can use a regular expression filter to eliminate the part of the string containing "(new)":

import re
def myfilter(x):
    return ' '.join(re.sub(ur'\(\s*[nN][eE][wW]\s*\)','',x).title().split())

wcity = myfilter( raw_input("Please enter a city ==> ") )
print wcity
wtype = myfilter( raw_input("Please enter a restaurant type ==> ") )
print wtype
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top