Formatting in Python 2
Python 2 doesn’t include the .format() method; instead you would use percent operators to mark the location of the variable being injected. The following is an example to inject the variable “name” into the location of “%s”. The letter after the percent operator signifies the data type. For integers, you would use “%d” for digit. After the string closes, you would place a percent operator, followed by the variables you would like to use. Let’s look at an example:
# one major difference between versions 2 & 3 name = 'John' print('Hello, %s' % name)
Go ahead and run that cell. You’ll notice that we get the same output as the previous methods. If you wanted to format a string in Python 2 with multiple variables, then you would need to write the following:
# python 2 multiple variable formatting first_name = "John" last_name = "Smith" print( "Hello, %s %s" % (first_name, last_name) ) # surround the variables in parenthesis
Go ahead and run the cell. We’ll get the output “Hello, John Smith”. When passing multiple variables, you need to surround the variable names within parenthesis and separate each by a comma. Notice there are also two symbols within the string that represent the location of each respective variable in order from left to right.
String Index
One other key concept that we need to understand about strings is how they are stored. When a computer saves a string into memory, each character within the string is assigned what we call an “index.” An index is essentially a location in memory. Think of
ChApTer 2 pyThoN BAsICs
an index as a position in a line that you’re waiting in at the mall. If you were at the front of the line, you would be given an index number of zero. The person behind you would be given index position one. The person behind them would be given index position two and so on.
Note Indexing in most languages, including python, starts at 0 not 1.
The same is true for Python strings. If we take a string like “Hello” and break down their indexes (see Figure 2-1), we can see that the letter “H” is located at index zero. Let’s try an example:
# using indexes to print each element word = "Hello" print( word[ 0 ] ) # will output 'H' print( word[ 1 ] ) # will output 'e' print( word[ -1 ] ) # will output 'o'
In order to index a specific element, you use square brackets to the right of the variable name. Within those square brackets, you put the index location you wish to access. In the preceding case, we’re accessing the first two elements in the string “Hello” stored in the variable “word”. The last line accesses the element in the last position. Using negative index numbers will result in trying to access information from the back, such that -4 would result in the output of the letter “e”.
Figure 2-1. Index locations for a string
Be very careful when working with indexes. An index is a specific location in memory. If you try to access a location that is out of range, you will crash your program because it’s trying to access a place in memory that does not exist. For example, if we tried to access index 5 on the “Hello”.
Do'stlaringiz bilan baham: |