Python endwith()具有多个字符串 [英] Python endswith() with multiple string

查看:747
本文介绍了Python endwith()具有多个字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个字符串:

myStr = "Chicago Blackhawks vs. New York Rangers"

我还有一个列表:

myList = ["Toronto Maple Leafs", "New York Rangers"]

使用endswith()方法,我想编写一个if语句,以检查myString是否以myList中的任一字符串结尾.我有基本的if语句,但是我对应该放在括号中的内容感到困惑.

Using the endswith() method, I want to write an if statement that checks to see if the myString has ends with either of the strings in the myList. I have the basic if statement, but I am confused on what I should put in the parentheses to check this.

if myStr.endswith():
    print("Success")

推荐答案

endswith()接受带后缀的元组.您可以将列表转换为元组,也可以只在第一位使用元组:

endswith() accepts a tuple of suffixes. You can either convert your list to a tuple or just use a tuple at the first place:

>>> myStr = "Chicago Blackhawks vs. New York Rangers"
>>> 
>>> my_suffixes = ("Toronto Maple Leafs", "New York Rangers")
>>> 
>>> myStr.endswith(my_suffixes)
True

str.endswith(后缀[,开始[,结束]])

如果字符串以指定的后缀结尾,则返回True,否则返回False. 后缀也可以是后缀的元组 为了.使用可选的启动,从该位置开始测试.和 可选端,请停止在该位置进行比较.

Return True if the string ends with the specified suffix, otherwise return False. suffix can also be a tuple of suffixes to look for. With optional start, test beginning at that position. With optional end, stop comparing at that position.

这篇关于Python endwith()具有多个字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆