Python endwith()具有多个字符串

编程入门 行业动态 更新时间:2024-10-23 18:24:14
本文介绍了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()具有多个字符串

本文发布于:2023-08-07 13:53:20,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1319409.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:多个   字符串   Python   endwith

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!