Python-如何验证字符串是否以特定字符串结尾? [英] Python- how to verify if a string ends with specific string?
问题描述
例如,我有以下字符串:' <代码>24499 ?00:02:05 sys-yg-ys'
I have the following string for example:
' 24499 ? 00:02:05 sys-yg-ys'
如何验证字符串是否以我从函数结果中得到的字符串结尾(例如 sys-yg-ys
)?
How can I verify if the string ends with a string which I got from a result of a function (e.g sys-yg-ys
)?
我在上面的字符串上尝试了以下(只是为了检查简单的情况):结果=''如果 (line.endswith('ys',len(line)-2,len(line)-1)):结果='真'
I tried the following (just to check easy case) on the string above: result='' if (line.endswith('ys',len(line)-2,len(line)-1)): result='true'
但是当我检查结果的值时我没有得到正确的结果.
but I didn't get true when I chech the value of result.
推荐答案
str.endswith() 函数 会做到这一点.例如:yourstring.endswith("sys-yg-ys")
The str.endswith() function will do this.
For example: yourstring.endswith("sys-yg-ys")
这篇关于Python-如何验证字符串是否以特定字符串结尾?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!