Skip to content Skip to sidebar Skip to footer

Lstrip Gets Rid Of Letter

With Python 2.7, I ran into the following problem: I have urls that I would like to clean, in particular I'd like to get rid of 'http://'. This works: >>> url = 'http://ww

Solution 1:

Think the argument of lstrip as characters, not a string.

url.lstrip('http://') removes all leading h, t, :, / from url.

Use str.replace instead:

>>>url = 'http://party.com'>>>url.replace('http://', '', 1)
'party.com'

If what you really want is get hostname from the url, you can also use urlparse.urlparse:

>>> urlparse.urlparse('http://party.com').netloc
'party.com'>>> urlparse.urlparse('http://party.com/path/to/some-resource').netloc
'party.com'

Post a Comment for "Lstrip Gets Rid Of Letter"