0

hey all... really in need of help, please! any feedback welcome!!!

i want to replace one keyword with multiple words.
i.e

replacement words =

'I went to the bakery to pick up the KEYWORD. When I got home I cut up the KEYWORD and gave some to the kids. I then put the KEYWORD in the freezer to keep it for later.'

I want to be able to replace the first KEYWORD with the first replacement word, 'wholegrain loaf', and then the remaining KEYWORD with the last replacement word, 'loaf'.

Any ideas?

Sorry if this is easy stuff... learning python currently - not very advanced yet... any help would be appreciated.

Thanks.

3
Contributors
2
Replies
3
Views
8 Years
Discussion Span
Last Post by jlm699
0

you counld use string.replace
For example:

words = 'hello i enjoy eating rasberry muffins'
words = words.replace('rasberry muffins','toasted bread')
print words
# this will output 'hello i enjoy eating toasted bread'

Hope this solves your problem. :)

0

Paulthom also forgot to mention that the replace function has an optional third parameter that will help you greatly:

>>> t = 'I love cheese;  cheese is my favorite thing to eat besides melted cheese.'
>>> t.replace('cheese', 'Shredded Gorgonzola', 1)
'I love Shredded Gorgonzola;  cheese is my favorite thing to eat besides melted cheese.'

The third parameter is count and I'm sure that you'll be able to deduce how to use it from my example. HTH

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.