Christopher Flach Christopher Flach - 1 year ago 100
Python Question

Python Case Matching Input and Output

I'm doing the pig latin question that I'm sure everyone here is familiar with it. The only thing I can't seem to get is matching the case of the input and output. For example, when the user enters Latin, my code produces

. I want it to produce

import string

punct = string.punctuation
punct += ' '
vowel = 'aeiouyAEIOUY'
consonant = 'bcdfghjklmnpqrstvwxzBCDFGHJKLMNPQRSTVWXZ'

final_word = input("Please enter a single word. ")
first_letter = final_word[:1]

index = 0

if any((p in punct) for p in final_word):
print("You did not enter a single word!")
while index < len(final_word) and (not final_word[index] in vowel):
index = index+1
if any((f in vowel) for f in first_letter):
print(final_word + 'yay')
elif index < len(final_word):

Answer Source

What you need is str.title(). Once you have done your piglatin conversion, you can use title() built-in function to produce the desired output, like so:

>>> "atinLay".title()

To check if a string is lower case, you can use str.islower(). Take a peek at the docs.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download