# single quotes 'Who said "to be or not to be"?' # double quotes "DNA goes from 5' to 3'." # escaped quotes "\"That's not fair!\" yelled my sister." # creates: "That's not fair!" yelled my sister # triple quoted strings, with single quotes '''This one string can go over several lines''' # "raw" strings, mostly used for regular expressions r"\"That's not fair!\" yelled my sister." # creates: \"That's not fair!\" yelled my sister # You can even have raw triple double quoted strings! r"""So there!"""You should usually use double quoted strings. Use raw strings for regular expressions and use triple quoted strings for long pieces of text (like doc strings).
You can tell the string to include a special character that you can't write. For example, "\n" is used to make a new line and "\t" is for the tab character. You can put a backslash before a quote character to escape it, and before a backslash to escape that as well.
>>> print "Line\t1\n\"Line 2\"\n\\Line 3\n" Line 1 "Line 2" \Line 3 >>>
Caution: Microsoft Windows uses path names like C:\Program Files\accessories\wordpad.exe. The \a is a special character (it makes a beep sound) so you should escape the \ as in "C:\\Program Files\\accessories\\wordpad.exe".
Normal character strings can only draw from a set of 256 characters. There are many more than 256 characters in the world (eg, with notations like α = 5 Å) which means some things can't be written using a simple (byte) string. Instead, people have agreed on Unicode strings, which extends the character space to up to 65536 characters and even more.
That said, I won't cover Unicode at all because it's too complicated for an introductory class when all the data files we'll use are written in the limited set of ASCII charaters.
123 # an integer 1.23 # a floating point number -1.23 # a negative floating point number 1.23E45; # scientific notation 0x7b; # hexadecimal notation (decimal 123) 0173; # octal notation (decimal 123) 12+3*j; # complex number 12 + 3i (Note that Python uses "j"!) 2147483648L # a long integer
>>> data = [1, 4, 9, 16] >>> data[0] 1 >>> data[1] 4 >>> data[2] = 7 >>> data [1, 4, 7, 16] >>> data[1:3] [4, 9] >>>
>>> data = (1, 4, 9, 16) >>> data[1] 4 >>> data[2] = 7 Traceback (most recent call last): File "", line 1, in ? TypeError: object doesn't support item assignment >>>
>>> d = {"A": "ALA", "C": "CYS", "D": "ASP"} >>> print d["A"] ALA >>>
We'll talk about lists and dictionaries more in the future.
This page based on Literals