Книга: Fedora™ Unleashed, 2008 edition

More on Strings

More on Strings

Python stores a string as an immutable sequence of characters — a jargon-filled way of saying "it is a collection of characters that, after they are 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 more 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:

>>> mystring = 'hello';
>>> myotherstring = "goodbye";
>>> mystring
'hello'
>>> 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 shows them both in single quotation marks because Python does not distinguish between the two. The third example is the exception — it 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 the string contains the single quotation mark.

Because the characters in a string are stored in sequence, you can index into them by specifying the character in which you are interested. 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'
>>> string[0]
'T'
>>> string [0], string[3], string [20]
('T', 's', 'g')

The last line shows how, with commas, you can ask for several indexes at the same time. You could print the entire first word by using this:

>>> string[0], string[1], string[2], string[3]
('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:

>>> string[0:4]
'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 copies either from the start or to the end:

>>> string [:4]
'This'
>>> string [5:]
'is a test string'
>>> string [11:]
'est string'

You can also omit both numbers, and it gives you the entire sequence:

>>> string [:]
'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 = "Python"
>>> mystring * 4
'PythonPythonPythonPython'
>>> mystring = mystring + " rocks! "
>>> mystring * 2
'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:

>>> string
'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 false — the string contains only alphanumeric characters, doesn't it? Well, no. There are spaces in there, which is what is causing the problem. More importantly, the calls were to upper() and lower(), and those methods did not change the contents of the string — they just returned the new value. So, to change the string from This is a test string to This Is A Test String, you actually have to assign it back to the string variable.

Оглавление книги

Оглавление статьи/книги

Генерация: 0.878. Запросов К БД/Cache: 3 / 0
поделиться
Вверх Вниз