If the following code equates to 'True':
'1234567' in '1234567:AMC'
Why won't the opposite also indicate 'True'? They both share the 1234567 character strings.
'1234567:AMC' in '1234567'
Is there a way this get this to say 'True' for the line of code above?
CodePudding user response:
The str
type has a .find()
method with can be used.
The method returns the starting position of the found string, or -1
if not found.
For example:
'1234567:AMC'.find('1234567')
Returns 0
. Indicating the primary string contains the substring starting at index 0. If the substring was not found, -1 would be returned.
CodePudding user response:
To say the second expression true, I would add a not
to flip it:
>>> '1234567:AMC' not in '1234567'
True
If you want to check if longer str
starts with input str
, I'd suggest str.startswith
:
>>> '1234567:AMC'.startswith('1234567')
True
But for contains check, I believe you hit the nail on the head:
>>> '1234567' in '1234567:AMC'
True