sara8
sara8

Reputation: 209

Python- how to verify if a string ends with specific string?

I have the following string for example: ' 24499 ? 00:02:05 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)?

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.

Upvotes: 4

Views: 8692

Answers (4)

ssnake
ssnake

Reputation: 375

try this:

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

output:

Before test: False
After test: True

why you want to add 'start' and 'end' parameter?

Upvotes: 2

ssnake
ssnake

Reputation: 375

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

Upvotes: 2

MNM
MNM

Reputation: 2743

you could use

 string.find("substring")

That should do it

Upvotes: -1

Thomite
Thomite

Reputation: 741

The str.endswith() function will do this. For example: yourstring.endswith("sys-yg-ys")

Upvotes: 18

Related Questions