Jared Nielsen wrote: > I implemented eryksun's suggestion and used the replace() method. > But, playing around with it, what I discovered is that it won't store the > change. > For example, when the input text is, "Ham and cheese or chicken and > waffles": > > #!/usr/bin/python > > text = raw_input("Enter text: ") > > print text.replace("and", "\nand").replace("or", "\nor") > > I get: > Ham > and cheese > or chicken > and waffles. > > But if I run the following: > > #!/usr/bin/python > > text = raw_input("Enter text: ") > > text.replace("and", "\nand") > text.replace("or", "\nor") > > print text > > I get the text as it was entered. > Is there a way to replace text in a string without splitting or > partitioning?
The replace() method does not modify the original string, it returns a new string with the appropriate replacements. With a line like > text.replace("and", "\nand") you throw away that new string. Change it to text = text.replace("and", "\nand") to keep it. However, I doubt you will be satisfied with the result of your script for long: >>> print raw_input().replace("and", "and\n") vandalizing androids wandering the wastelands vand alizing and roids wand ering the wasteland s _______________________________________________ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor