【问题标题】:How can I do variable.endswith("a" or "b" or "c"....) : doAThing in Python? [duplicate]我该怎么做 variable.endswith("a" or "b" or "c"....) : doAThing in Python? [复制]
【发布时间】:2020-01-14 11:48:11
【问题描述】:

我正在开发一款处于早期阶段的翻译软件。我正在尝试获取单词的结尾字符并对其进行处理,但我需要区分不同的情况。

如何使用if endswith("a" or "b" or "c") : doAThing

我试过|||=or

if word.endswith("a" , "e" , "i" , "o" , "u") : print("JEFF")

错误:

 File "test.py", line 50, in <module>
    if word.endswith("a" , "e" , "i" , "o" , "u") and not ("a" , "e" , "i" , "o" , "u"): print("JEFF")
TypeError: endswith() takes at most 3 arguments (5 given)

【问题讨论】:

    标签: python python-3.x ends-with


    【解决方案1】:

    如果您检查docs for the 'endswith' function,您将看到函数签名。它可以采用一个字符串或一个 tuple 字符串来匹配。

    您传递参数的方式是 5 个不同的参数。要创建一个元组(和一个参数),请将字符串括在括号中。

    if word.endswith(("a" , "e" , "i" , "o" , "u")):
        print("JEFF")
    

    【讨论】:

      猜你喜欢
      • 2015-11-20
      • 2017-01-11
      • 2018-01-29
      • 2015-12-31
      • 2015-11-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多