Reputation: 4256
I'm a bit embarrassed to ask:
but why won't this successfully work? the .statswith method is fine here, returning True, but i can't get this replace to work...
sandwich = "Au Cheval Cheeseburger"
restaurant = "Au Cheval"
if sandwich.startswith(restaurant):
sandwich.replace(restaurant, "")
print sandwich
(if you can't tell, i want sandwich to just read Cheeseburger. Is there also a more efficient way to do this than going thru string replace?
thanks everyone.
EDIT: God i knew it was something silly. It's late and the brain's fried. thanks everyone.
Upvotes: 2
Views: 175
Reputation: 8335
You were doing fine but till the replace method
The replace methods just replaces and provides the string it does not store it in the variable
Since you did not not store it the variable is not changed
sandwich = "Au Cheval Cheeseburger"
restaurant = "Au Cheval"
if sandwich.startswith(restaurant):
sandwich=sandwich.replace(restaurant, "")
print sandwich
previously you did not save it
sandwich.replace(restaurant, "")
But to save the replacement changes you have to save it
sandwich=sandwich.replace(restaurant, "")
Upvotes: 1
Reputation: 1359
sandwich = "Au Cheval Cheeseburger"
restaurant = "Au Cheval"
if sandwich.startswith(restaurant):
sandwich = sandwich.replace(restaurant, "")
print sandwich
Upvotes: 0
Reputation: 28253
string replacement returns a copy.
sandwich = sandwich.replace(restaurant, "")
Upvotes: 3
Reputation: 1314
From http://www.tutorialspoint.com/python/string_replace.htm
The method replace() returns a copy of the string in which the occurrences of old have been replaced with new
Try sandwich = sandwich.replace(restaurant, "")
Upvotes: 6