| Nothing Can Stop Me Now ( @ 2008-06-20 20:53:00 |
| Current music: | "Never Is A Promise," Fiona Apple |
| Entry tags: | backslash, backticks, concatenating, escaping, long strings, python, quotes, raw input, repr, str, strings |
Python 101: Strings
Strings
Remember waaaaaay back when we did
>>> print "Make tea, not love." Make tea, not love. >>>
Well, print is a command, and "Make tea, not love." is a string! Strings are all over the damn place, and, as we used them here, one use is to represent a bit of text. Why the quotes? Why not! It gave us an error back in the day of a few hours ago when I tried it without, if you recall. Strings are values, and all sorts of fun can be had with them, and we will, a bit later. For now, why the quotes? Will single quotes work?
>>> print 'Make tea, not love.' Make tea, not love. >>>
Ayup! So what's the difference? Well, the short answer here is there isn't one. You do run into some odd bits though, if you're not paying attention.
>>> print 'I'll make us some tea!' SyntaxError: invalid syntax >>>
What the hell, you ask. I can hear it. The problem here is that the apostrophe is funking with your heart, I mean your code. Here, the actual string only contains one letter ('I') and all the rest afterwards, it just doesn't know what to do with it. You get the same booboo if you're trying to use quotes within a string, since Python thinks it's over before it really is. There's still one more trick here. What if we had a string containing both single and double quotes?
Well, welcome to escaping quotes 101!
>>> print '"I\'ll go make some tea," she said.' "I'll go make some tea," she said. >>>
Backslash it. There are other ways of coping, but we'll deal with them later.
You can even add strings together! This is called concatenating them. However, concatenating strings only work when both strings are written at the same time. But what if you have two separately generated strings and you NEED to smoosh them all up in each other's faces right away?
>>> "Make tea, " "not love." 'Make tea, not love.' >>> >>> x="Make tea, " >>> y="not love." >>> x y SyntaxError: invalid syntax >>> >>> x+y 'Make tea, not love.' >>>
You add them!
str, repr and Backticks
Coming soon.
input Versus raw_input
I'm a bad bad weekend tranny for not subjecting you all to raw_input until now.
input assumes whatever you enter is a valid Python expression, whereas raw_input takes the data and puts it into a string. Really, unless you seriously absolutely NEED input, stick with raw_input.
>>> raw_input("Make tea: ")
Make tea: not love!
'not love!'
>>> Last sneaky thing about strings are long strings. Long strings are the same thing as any other string, except they span more than one line. To cap them all together, you can use triple quotes as follows.
>>> print """Make tea, not love.""" Make tea, not love. >>>
You can also use '''. Because the quotes are distinctive, you can use single and double quotes inside them without having to escape them. Backslash can be used to escape line breaks in short strings, if needed. Another handy trick: Let's say you need a backslash in a quote, then what? You can escape the backslash!
>>> print "This is me escaping the \\ backslash!" This is me escaping the \ backslash! >>>
There's even a niftier way of doing this, if you need several backslashes in a string. These are called raw strings and they are written like this.
>>> print r"This\is\a\raw\string." This\is\a\raw\string. >>>
The downside to raw strings is that if you have to escape a quote, you will end up with an unwanted backslash in your final string. And the last character of a raw string cannot be a backslash, because Python doesn't understand if you're trying to end the string or not. The way around this if you ABSOLUTELY need that final backslash is to simply contain the backslash in a seperate string.
>>> print r"Make\tea\not\love" "\\" Make\tea\not\love\ >>>