[Tutor] how to split/partition a string on keywords?
Jared Nielsen
nielsen.jared at gmail.com
Thu Aug 23 21:05:38 CEST 2012
Hi all,
I'm new to programming and Python.
I want to write a script that takes a string input and breaks the string at
keywords then outputs the pieces on separate lines.
I'm not sure how to break the string, though.
I looked through the docs and found split() and partition(), which come
close.
But split() doesn't retain the separator and partition() retains the white
space and returns a 3-tuple which I'll have to figure out how to rejoin nor
does it partition on subsequent instances of the separator.
Here's the script in its basic form:
#!/usr/bin/python
text = raw_input("Enter text: ")
print "You entered ", text
objects = text.partition(' and')
print objects
for object in objects: # Second Example
print object
For example, if I run this with the input:
"Ham and cheese omelette with hasbrowns and coffee."
I get:
Ham
and
cheese omelette with hashbrowns and coffee.
Any help is greatly appreciated.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20120823/a8adaeeb/attachment-0001.html>
More information about the Tutor
mailing list