Python 如果字符串是';e';

Python 如果字符串是';e';,python,string,Python,String,我正在创建一个程序,将一个普通的英语单词转换成一种拉丁语形式。我需要能够确定字符串是否以“e”结尾(最后一个字符),如果以“e”结尾,则将其替换为ë 我似乎无法使用我的函数让它工作。例如,在该条件结束时,代码应将单词“happy”输出为“appyhë” # User Input: Ask user for a word WordToBeTranslated = input("Please enter a word in English: ") WordToBeTranslatedLower =

我正在创建一个程序,将一个普通的英语单词转换成一种拉丁语形式。我需要能够确定字符串是否以“e”结尾(最后一个字符),如果以“e”结尾,则将其替换为ë

我似乎无法使用我的函数让它工作。例如,在该条件结束时,代码应将单词“happy”输出为“appyhë”

# User Input: Ask user for a word

WordToBeTranslated = input("Please enter a word in English: ")
WordToBeTranslatedLower = WordToBeTranslated.lower()

# Condition #1: Moving the First Letter to the end

elvish = WordToBeTranslatedLower[1:] + WordToBeTranslatedLower[0]
print(elvish)

# Condition #2 + #3: Appending a Vowel / Appending 'en' to the end of a word

vowel = ['a', 'e', 'e', 'i', 'o', 'u']
import random
randomVowel = random.choice(vowel)
list = []
list.append(WordToBeTranslated)
if len(WordToBeTranslated) > 4:
    elvish += randomVowel

else:
    elvish = elvish + 'en'

# Condition #4: change all k's to c's

elvish = elvish.replace('k', 'c')
print(elvish)

# Condition #5: Replace 'e' at end of the word with ë

if elvish[-1] == 'e':
    elvish = elvish[-1].replace('e', 'ë')
else:
    elvish = elvish
您可以尝试:

your_string.endswith("e")
您还可以使用正则表达式将“e”替换为“ë”

此代码:

elvish = elvish[-1].replace('e', 'ë')
根本不做你想做的事。它只会用最后一个字母重新分配
精灵语
,必要时替换e

现在,在这个if块中,您知道最后一个字母是e,所以您总是需要替换它。然后您要做的是将原始字符串减去最后一个字母,并附加
ë
。因此:

elvish = elvish[:-1] + 'ë'

另外,您不需要else块;您的块不起任何作用,您可以将其移除。

您到底被卡在哪里?为什么您认为,在条件2之后,总是会附加一个
e
elvish = elvish[:-1] + 'ë'