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'
#1
The title makes this really confusing. Hopefully it isn't.

In the title I used 'X' and 'Y'. 'X' is a string url: https://i.imgur.com/7Ic4AMO.gifv and 'Y' is what I want to check for in the string.

Take this code:
if '.gif' in X:
    print("HELLO")
elif '.gifv' in X:
    print("PLEASE PRINT THIS")
No matter if it has '.gif' or '.gifv' in the url, it will always print 'HELLO'. That occurs because '.gifv' also includes '.gif' so it never makes it to the next part of the if statement.
I have also tried:
X.endswith('.gifv') / X.endswith('.gif')
but it always returns false.

How can I work around this? Would changing the order of the if statement work?

Thanks,
Dream
Reply


Messages In This Thread
Python: if 'X' in 'Y' but with two similar strings as 'X' - by DreamingInsanity - Jan-31-2019, 09:32 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Trying to understand strings and lists of strings Konstantin23 2 834 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,021 Jun-29-2021, 06:58 AM
Last Post: tester_V
  Finding multiple strings between the two same strings Slither 1 2,560 Jun-05-2019, 09:02 PM
Last Post: Yoriz
  Splitting strings in python? NLittle17 3 2,436 Jan-05-2019, 09:20 AM
Last Post: Axel_Erfurt
  Similar to Poker bluekade5050 1 35,743 Nov-14-2018, 04:46 PM
Last Post: j.crater
  lists, strings, and byte strings Skaperen 2 4,277 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