Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python how to remove http from a string using replace [duplicate]

I would like to remove "http://" in a string that contains a web URL i.e: "http://www.google.com". My code is:

import os
s = 'http://www.google.com'
s.replace("http://","")
print s

I try to replace http:// with a space but somehow it still prints out http://www.google.com

Am i using replace incorrectly here? Thanks for your answer.

like image 401
Kiddo Avatar asked Apr 05 '14 04:04

Kiddo


1 Answers

Strings are immutable. That means none of their methods change the existing string - rather, they give you back a new one. So, you need to assign the result back to a variable (the same, or a different, one):

s = 'http://www.google.com'
s = s.replace("http://","")
print s
like image 68
lvc Avatar answered Sep 18 '22 16:09

lvc