15.04.2013 Views

Core Python Programming (2nd Edition)

Core Python Programming (2nd Edition)

Core Python Programming (2nd Edition)

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

>>> quest.endswith('color?')<br />

True<br />

>>><br />

>>> quest.find('or', 30)<br />

-1<br />

>>><br />

>>> quest.find('or', 22)<br />

25<br />

>><br />

>>> quest.index('or', 10)<br />

16<br />

>>><br />

>>> ':'.join(quest.split())<br />

'what:is:your:favorite:color?'<br />

>>> quest.replace('favorite color', 'quest')<br />

>>><br />

'what is your quest?'<br />

>>><br />

>>> quest.upper()<br />

'WHAT IS YOUR FAVORITE COLOR?'<br />

The most complex example shown above is the one with split() and join(). We first call split() on our<br />

string, which, without an argument, will break apart our string using spaces as the delimiter. We then<br />

take this list of words and call join() to merge our words again, but with a new delimiter, the colon.<br />

Notice that we used the split() method for our string to turn it into a list, and then, we used the join()<br />

method for ':' to merge together the contents of the list.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!