如何从字符串中去掉特定的字母

2024-03-29 01:57:32 发布

您现在位置:Python中文网/ 问答频道 /正文

我很难创建一个以两种不同方式删除字母“T”的函数。parameter是一个Boolean,如果参数是True,那么它将打印出一个userinput(这是在我下面的代码中编写的),它将一直请求相同的输入,直到用户输入“quit”,当用户输入“quit”时,它将去掉字符串中所有小写的“t”。如果BooleanFalse,那么同样的过程也适用,但它不是去掉小写字母“t”,而是去掉大写字母“t”。另外,我想保留我在下面代码中编写的格式类型,谢谢。顺便说一句,我使用的是python2.4。在

def removeT(Boolea):
    userInput=str(input("Enter a word/sentence you want to process: "))
    while userInput:
        string = (raw_input("Enter a word/sentence you want to process:"))
        if Boolea == False:
            userInput = userInput + string
            while "T" in userInput:
                index = userInput.find("T")
                userInput = userInput[:index] + userInput[index+1:]
        if string == "quit":
            keepAsking = False
            return userInput

        else:
            userInput = userInput + string
            while "t" in userInput:
                index = userInput.find("t")
                userInput = userInput[:index] + userInput[index+1:]
            while "T" in userInput:
                index = userInput.find("T")
                userInput = userInput[:index] + userInput[index+1:]

Tags: 代码用户infalseinputstringindexfind