Python Forum
Python: if 'X' in 'Y' but with two similar strings as 'X'
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python: if 'X' in 'Y' but with two similar strings as 'X'
#5
if you want to check string end, better use some_string.endswith(search_string). in checks for membership, so it will return True even if the search_string is in the middle of some_string. .endswith() is also recommened by PEP8 compared to using slicing.
Alternative approach is using something like

def spam():
    print('Doing something within spam()')
    
def eggs():
    print('Doing something within eggs()')
    
options = (('gifv', spam), ('gif', eggs))

some_string = 'foo.gifv'
for end, func in options:
    if some_string.endswith(end):
        func()
        break
This way it will allow to expand the list of options without ending with monstrous if/elif/elif/.../else block.
Options can be dict in python 3.7 or collections.OrderedDict (in version before that) in order to preserve the order in which will be checked
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Messages In This Thread
RE: Python: if 'X' in 'Y' but with two similar strings as 'X' - by buran - Jan-31-2019, 10:59 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Trying to understand strings and lists of strings Konstantin23 2 833 Aug-06-2023, 11:42 AM
Last Post: deanhystad
  Splitting strings in list of strings jesse68 3 1,831 Mar-02-2022, 05:15 PM
Last Post: DeaD_EyE
  Sum similar items tester_V 3 2,019 Jun-29-2021, 06:58 AM
Last Post: tester_V
  Finding multiple strings between the two same strings Slither 1 2,559 Jun-05-2019, 09:02 PM
Last Post: Yoriz
  Splitting strings in python? NLittle17 3 2,433 Jan-05-2019, 09:20 AM
Last Post: Axel_Erfurt
  Similar to Poker bluekade5050 1 35,605 Nov-14-2018, 04:46 PM
Last Post: j.crater
  lists, strings, and byte strings Skaperen 2 4,276 Mar-02-2018, 02:12 AM
Last Post: Skaperen

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020