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'
#6
(Jan-31-2019, 10:13 AM)perfringo Wrote:
(Jan-31-2019, 09:42 AM)DreamingInsanity Wrote: I got the idea at the time of posting but waited to see if there were other ways.

There are many other ways how to do it (including your endswith).

However, suggesting the 'other ways' one must know what is the end goal (what do you want accomplish). For printing out this solution is good enough. But your objective is probably not to just print out something. If there is match you probably want to act upon this information. Therefore to present 'other ways' one must know what you want to do after match is found.

The only part that wasnt included in the code was that once it found what ending the url had, it would call a function and it would download the file from that url. I needed the ending so I could save it with that specific ending.
urllib.request.urlretrieve(url, img_dir + str(name) + '.gif') / urllib.request.urlretrieve(url, img_dir + str(name) + '.gifv')  
Thanks,
Dream

(Jan-31-2019, 10:59 AM)buran Wrote: 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

Thanks, I will use this in the future!

Dream
Reply


Messages In This Thread
RE: Python: if 'X' in 'Y' but with two similar strings as 'X' - by DreamingInsanity - Feb-01-2019, 09:47 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Can you explain the strings in Python ebn852_pan 3 276 May-19-2024, 08:36 AM
Last Post: Pedroski55
  Trying to understand strings and lists of strings Konstantin23 2 896 Aug-06-2023, 11:42 AM
Last Post: deanhystad
  Splitting strings in list of strings jesse68 3 1,889 Mar-02-2022, 05:15 PM
Last Post: DeaD_EyE
  Sum similar items tester_V 3 2,054 Jun-29-2021, 06:58 AM
Last Post: tester_V
  Finding multiple strings between the two same strings Slither 1 2,601 Jun-05-2019, 09:02 PM
Last Post: Yoriz
  Splitting strings in python? NLittle17 3 2,465 Jan-05-2019, 09:20 AM
Last Post: Axel_Erfurt
  Similar to Poker bluekade5050 1 37,831 Nov-14-2018, 04:46 PM
Last Post: j.crater
  lists, strings, and byte strings Skaperen 2 4,317 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