Python: Stringing You Along (
Page 1 of 4 )
If you have ever programmed before, or are a ninety-year-old lady, or a cat, then you know how useful strings are. If you have been reading these articles, you will know that a string is a character, a sentence, a paragraph, a book, etc. If you haven't been reading these articles, then you're missing an important part of your education as a programmer. Don't keep yourself in the dark; start reading!In this epic article we will cover the various ways to manipulate string objects in Python. These range from sorting and indexing to some of the built-in string methods. So put down those knitting tools, kick that cat across the room like a football, and let's be like Olivia Newton-John (i.e., let's get physical).
The Basics
Here we are going to cover some of the basics of strings. If you need a refresher on them, you can read along. Or, if you are a string writing pro, feel free to skip over this section, or read it and get real bored and send me hate mail. Whichever you choose is fine.
Printing a string
#!/usr/local/bin/python
print "Here are some samples of basic strings:"
print " "
print "Now you know and knowing is half the battle"
print "A"
print "9"
print "%#$@!"
print "It was the best of times, it was the worst of times,"
print "It was the age of wisdom, it was the age of foolishness"
print "It was the epoch of belief, it was the epoch of incredulity"
print "It was the season of Light, it was the season of Darkness"
print "This quote goes on forever..."
Storing Strings in Variables and Printing Them
#!/usr/local/bin/python
apple = "Fudgebuckets"
sentence="Look at this! It's a sentence! No wait...it's two sentences!"
print apple
print sentence
Printing Quoted Text
#!/usr/local/bin/python
print "And he said to me, 'I pity the fool!' "
print 'And I quote: "Quit yo jibba-jabba!"'
print " "
print "Notice that it is best to use single quotes when you want to do double quotes"
print "And double quotes when you want to do single quotes"