Python-如何验证字符串是否以特定字符串结尾?

时间:2016-04-21 07:59:37

标签: python string

我有以下字符串例如: '24499 ? 00:02:05 sys-yg-ys'

如何验证字符串是否以函数结果中的字符串结尾(例如sys-yg-ys)?

我在上面的字符串上尝试了以下(只是为了检查简单的情况):     结果=“”     if(line.endswith('ys',len(line)-2,len(line)-1)):             结果= '真'

但是当我知道结果的价值时,我没有成真。

4 个答案:

答案 0 :(得分:12)

this guest webinar with IntelliJ会这样做。 例如:def kickoff(clients): logging.info("Going through clients.csv") for host in clients: batkickoff = subprocess.call('C:\pstools\psexec.exe \\\\%s -d C:\test.bat' % hostname) if batkickoff != 1: print "there was an issue" logging.info(hostname + " had an issue kicking off") failedclients.append(hostname) kickoff(clients)

答案 1 :(得分:1)

re.match(r"^.+(sys-yg-ys)$", string)

答案 2 :(得分:0)

试试这个:

line = '  24499 ?   00:02:05 sys-yg-ys'
result = False
print("Before test: " + str(result))
result = line.endswith('ys')
print("After test: " + str(result))

输出:

Before test: False
After test: True

为什么要添加'start'和'end'参数?

答案 3 :(得分:-1)

你可以使用

 string.find("substring")

应该这样做