Python stores strings as an immutable sequence of charactersa jargon-filled way of saying that "it is a collection of characters that, once set, cannot be changed without creating a new string." Sequences are important in Python. There are three primary types, of which strings are one, and they share some properties. Mutability makes much sense when you learn about lists in the next section.
As you saw in the previous example, you can assign a value to strings in Python with just an equal sign, like this:
>>> myotherstring; 'goodbye'
>>> test = "Are you really Bill O'Reilly?" >>> test
"Are you really Bill O'Reilly?"
The first example encapsulates the string in single quotation marks, and the second and third in double quotation marks. However, printing the first and second strings show them both in single quotation marks because Python does not distinguish between the two. The third example is the exceptionit uses double quotation marks because the string itself contains a single quotation mark. Here, Python prints the string with double quotation marks because it knows it contains the single quotation mark.
Because the characters in a string are stored in sequence, you can index into them by specifying the character you are interested in. Like most other languages, these indexes are zero based, which means you need to ask for character 0 to get the first letter in a string. For example:
>>> string = "This is a test string" >>> string
'This is a test string'
The last line shows how, with commas, you can ask for several indexes at the same time. You could print the entire first word using this:
>>> string, string, string, string ('T', 'h', 'i', 's')
However, for that purpose you can use a different concept: slicing. A slice of a sequence draws a selection of indexes. For example, you can pull out the first word like this:
The syntax there means "take everything from position 0 (including 0) and end at position 4 (excluding it)." So, [0:4] copies the items at indexes 0, 1, 2, and 3. You can omit either side of the indexes, and it will copy either from the start or to the end:
>>> string [5:] 'is a test string' >>> string [11:] 'est string'
You can also omit both numbers, and it will give you the entire sequence:
'This is a test string'
Later you will learn precisely why you would want to do that, but for now there are a number of other string intrinsics that will make your life easier. For example, you can use the + and * operators to concatenate (join) and repeat strings, like this:
>>> mystring = mystring + " rocks! "
'Python rocks! Python rocks! '
In addition to working with operators, Python strings come with a selection of built-in methods. You can change the case of the letters with capitalize() (uppercases the first letter and lowercases the rest), lower() (lowercases them all), title() (uppercases the first letter in each word), and upper() (uppercases them all). You can also check whether strings match certain cases with islower(), istitle(), and isupper(); that also extends to isalnum() (returns true if the string is letters and numbers only) and isdigit() (returns true if the string is all numbers).
This example demonstrates some of these in action:
'This is a test string' >>> string.upper() 'THIS IS A TEST STRING' >>> string.lower() 'this is a test string' >>> string.isalnum() False
>>> string = string.title() >>> string
'This Is A Test String'
Why did isalnum() return falseour string contains only alphanumeric characters, doesn't it? Well, no.
There are spaces in there, which is what is causing the problem. More important, we were calling upper() and iower() and they were not changing the contents of the stringthey just returned the new value. So, to change our string from This is a test string to This Is A Test String, we actually have to assign it back to the string variable.
Was this article helpful?