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,420 points
37 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,870 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,520 points
783 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
221 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,230 points
248 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 4, 2019 in Python by Srisha
909 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 6, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 1,833 views
0 votes
0 answers
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,870 points
105 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 in Python by Gitika
• 65,870 points
46 views