lundi 4 juillet 2016

Stripping multiple characters from the start of a string

I'm trying to trim a sub-string from the beginning of a string based on a condition:

For instance, if the input is a domain name prefixed with http, https and/or www, it needs to strip these and return only the domain.

Here's what I have so far:

if my_domain.startswith("http://"):
        my_domain = my_domain[7:]
elif my_domain.startswith("https://"):
        my_domain = my_domain[8:]

if my_domain.startswith("www."):
        my_domain = my_domain[4:]

print my_domain

I've tried to use these inbuilt functions (.startswith) instead of trying to use regex.

While the code above works, I'm wondering if there is a more efficient way to combine the conditions to make the code shorter or have multiple checks in the same conditional statement?

Aucun commentaire:

Enregistrer un commentaire