>>> 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.

Lists

Python's built-in list data type is a sequence, like strings. However, Python's lists are mutable, which means they can be changed. Lists are like arrays in that they hold a selection of elements in a given order. You can cycle through them, index into them, and slice them:

>>> mylist = ['python', 'perl', 'php']

>>> mylist

['python', 'perl', 'php']

>>> mylist + ['java']

['python', 'perl', 'php', 'java']

>>> mylist * 2

['python', 'perl', 'php', 'python', 'perl', 'php']

>>> mylist[1]

'perl'

>>> mylist[1] = 'c++'

>>> mylist[1]

'c++'

>>> mylist[1:3] ['c++', 'php']

The brackets notation is important: You cannot use parentheses (( and )) or braces ({ and }) for lists. Using + for lists is different from using + for numbers. Python detects you are working with a list and appends one list to another. This is known as operator overloading, and it is one of the reasons Python is so flexible.

Lists can be nested, which means you can put a list inside a list. However, this is where mutability starts to matter, and so this might sound complicated! If you recall, the definition of an immutable string sequence is a collection of characters that, after they are set, cannot be changed without creating a new string. Lists are mutable, as opposed to immutable, which means you can change your list without creating a new list.

This becomes important because Python, by default, copies only a reference to a variable rather than the full variable. For example:

>>> list1 = [1, 2, 3]

>>> list2 = [4, list1, 6]

>>> list1

[1, 2, 3]

>>> list2

[4, [1, 2, 3], 6]

Here you can see a nested list. list2 contains 4, and then list1, and then 6. When you print the value of list2, you can see it also contains list1. Now, proceeding on from that:

>>> list1[1] = 'Flake'

>>> list2

[4, [1, 'Flake', 3], 6]

Line one sets the second element in list1 (remember, sequences are zero-based!) to be Flake rather than 2; then the contents of list2 are printed. As you can see, when list1 changed, list2 was updated also. The reason for this is that list2 stores a reference to list1 as opposed to a copy of list1; they share the same value.

You can show that this works both ways by indexing twice into list2, like this:

>>> list2[1][1] = 'Caramello'

>>> list1

[1, 'Caramello', 3]

The first line says, 'get the second element in list2 (list1) and the second element of that list, and set it to be 'Caramello'.' Then list1's value is printed, and you can see it has changed. This is the essence of mutability: We are changing our list without creating a new list. On the other hand, editing a string creates a new string, leaving the old one unaltered. For example:

>>> mystring = 'hello'

>>> list3 = [1, mystring, 3]

>>> list3

Добавить отзыв
ВСЕ ОТЗЫВЫ О КНИГЕ В ИЗБРАННОЕ

0

Вы можете отметить интересные вам фрагменты текста, которые будут доступны по уникальной ссылке в адресной строке браузера.

Отметить Добавить цитату