Python Projects for Beginners a ten-Week Bootcamp Approach to Python Programming



Download 2,61 Mb.
bet33/200
Sana20.06.2022
Hajmi2,61 Mb.
#681748
1   ...   29   30   31   32   33   34   35   36   ...   200
Bog'liq
Python Projects for Beginners A Ten Week Bootcamp Approach to Python

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

Download 2,61 Mb.

Do'stlaringiz bilan baham:
1   ...   29   30   31   32   33   34   35   36   ...   200




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2024
ma'muriyatiga murojaat qiling

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish