Python案例匹配输入和输出

时间:2016-10-06 14:37:47

标签: python string

我正在做猪拉丁语问题,我确信这里的每个人都熟悉它。我唯一能做的就是匹配输入和输出的情况。例如,当用户输入拉丁语时,我的代码会生成atinLay。我想要它生成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')

2 个答案:

答案 0 :(得分:1)

您需要的是str.title()。完成Piglatin转换后,您可以使用title()内置函数生成所需的输出,如下所示:

>>> "atinLay".title()
'Atinlay'

要检查字符串是否为小写,可以使用str.islower()。看看文档。

答案 1 :(得分:0)

只需使用内置的字符串函数。

s = "Hello".lower()
s == "hello"
s = "hello".upper()
s == "HELLO"
s = "elloHay".title()
s == "Ellohay"