Python Forum
a better way to code this to fix a URL?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
a better way to code this to fix a URL?
#1
i have this silly code meant to fix a URL that is missing as many as 7 start characters:
if url.startswith('//'): url = 'https:' + url
if url.startswith('://'): url = 'https' + url
if url.startswith('s://'): url = 'http' + url
if url.startswith('ps://'): url = 'htt' + url
if url.startswith('tps://'): url = 'ht' + url
if url.startswith('ttps://'): url = 'h' + url
is there a nice clean and pythonic way to refactor this code into 3 lines or fewer?

i hope you got a good laugh out of that code above.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply


Messages In This Thread
a better way to code this to fix a URL? - by Skaperen - Mar-19-2024, 05:10 AM

Forum Jump:

User Panel Messages

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