How to compare two string with some characters only in python

0 votes

I have two strings to compare and the following result should return

s1 = 'toyota innova'
s2 = 'toyota innova 7'
if s1 like s2
   return true

OR

s1 = 'tempo traveller'
s2 = 'tempo traveller 15 str'  //or tempo traveller 17 str
if s1 like s2
    return true

In this it is showing that we don't find this model name but if you scroll down there is tempo traveller 12str/15str showing. so I have show these two cabs in search of tempo traveller.

Nov 26, 2020 in Python by Roshni
• 10,520 points
576 views

1 answer to this question.

0 votes

You could use in to check if a string is contained in another:

'toyota innova' in 'toyota innova 7' # True
'tempo traveller' in 'tempo traveller 15 str' # True

If you only want to match the start of the string, you can use str.startswith:

'toyota innova 7'.startswith('toyota innova') # True
'tempo traveller 15 str'.startswith('tempo traveller') # True

Alternatively, if you only want to match the end of the string, you can use str.endswith

'test with a test'.endswith('with a test') # True
answered Nov 26, 2020 by Gitika
• 65,910 points

Related Questions In Python

0 votes
1 answer

How to parse date/time string with timezone abbreviated name in Python?

The parse() function in dateutil can't handle ...READ MORE

answered Nov 27, 2018 in Python by Nymeria
• 3,560 points
1,932 views
0 votes
1 answer

how do i print only the last two letters in a string using regular expression in python?

$ to match the end of the ...READ MORE

answered Mar 15, 2019 in Python by Waseem
• 4,540 points
690 views
0 votes
1 answer

how to compare two strings in python?

compare two string in python >>> s1="abc ...READ MORE

answered Mar 25, 2019 in Python by rajesh
• 1,270 points
905 views
0 votes
1 answer

How to check if a string ends with a character in python?

You can use the endswith method. It checks ...READ MORE

answered Apr 5, 2019 in Python by Srisha
2,624 views
0 votes
2 answers
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 7, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 4,058 views
0 votes
1 answer
0 votes
1 answer

How to create a unicode string in python with the string eg: This is a string?

Hey, @Roshni, It is very simple to execute, ...READ MORE

answered Jun 23, 2020 in Python by Gitika
• 65,910 points
525 views
0 votes
1 answer

How to Remove specific characters from a string in Python?

Strings in Python are immutable (can't be changed). Because ...READ MORE

answered Jan 5, 2021 in Python by Gitika
• 65,910 points
678 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP