Christopher Flach Christopher Flach - 2 months ago 6
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

atinLay
. I want it to produce
Atinlay
.

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!")
else:
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):
print(final_word[index:]+final_word[:index]+'ay')

Answer

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()
'Atinlay'

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