user5678956
user5678956

Reputation: 63

Finding if string is substring of some string in python: a special case

I am working on finding if a string is sub-string of some string in python. It runs fine for all the cases but here is a special case where it fails:

In python shell I do:

x=repr('\r\r\r') 
y=repr('\r\r') 

In this case y in x returns false. Can someone tell me what to do so that it returns true?

Upvotes: 2

Views: 118

Answers (1)

NPE
NPE

Reputation: 500167

There is no special case here. The reason this doesn't work is that y contains a leading and a trailing single quote:

>>> x=repr('\r\r\r')
>>> y=repr('\r\r')
>>> x
"'\\r\\r\\r'"
>>> y
"'\\r\\r'"
        ↑ This character is not in `x'

It's not entirely clear how you want this to work, but you could remove the quotes before testing the condition:

>>> y.strip("'")
'\\r\\r'
>>> y.strip("'") in x
True

This is probably a hack rather than a solution, but it's hard to propose a good solution without knowing the actual problem you're trying to solve.

Upvotes: 2

Related Questions