Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (19.9k points)

I'm setting up a script and I need to return a bool when a string is detected in another string.

I've tried to use the function str.find() but it doesn't fit with what I want to obtain.

str1 = 'test*'

str2 = 'test12345'

How can I return a bool if 'test' is in str2 by taking in consideration that '*' is everything after the string 'test'.

I've tried to use .find() as:

str2.find(str1.replace('*','')

I'm trying to search a easier way to do that.

Thanks in advance.

1 Answer

0 votes
by (25.1k points)

What you can do in this case is replace the ‘*’ character with an empty string ’’ and check if a string starts with the current string with the asterisk ‘*’ removed.

You can use this code:

str1 = 'test*' 

str2 = 'test12345' 

str.startswith(str1.replace('*',''))

Browse Categories

...