如何在函数通过列表后调用函数时更改函数中的字符串?

时间:2018-03-14 15:48:08

标签: python python-3.x basehttpserver

在main()中调用它时,有没有办法在cat()中改变单词cat to dog?

例如:

cut -c -5,9- <file>
cut --complement -c 6-8 <file>

1 个答案:

答案 0 :(得分:2)

方法replace返回原始字符串的副本,其中所有出现的子字符串'cat'都被'dog'替换。您可以将此新字符串分配给原始变量。 Refer Python Documentation for usage of replace

def notimportant2():
    pass

def notimportant1():
    pass

def string():
    return ["This cat was scared."]


def contentList(skip_name=''):
    functions = [notimportant1, notimportant2, string]
    for f in functions:
        if f.__name__ != skip_name:
            f()

def main():
    contentList('notimportant2') 
    for words in string():
      words=words.replace("cat", "dog")
      print(words)

main()