Professional IronPython


MyString = ‘Hello {0[name]}‘



Download 20,35 Mb.
Pdf ko'rish
bet106/453
Sana01.07.2022
Hajmi20,35 Mb.
#723045
1   ...   102   103   104   105   106   107   108   109   ...   453
MyString = ‘Hello {0[name]}‘
: Provides a replacement from a dictionary. The correspond-
ing 
format()
method input is 
MyString.format({‘name‘:‘George‘})
. Of course, you can 
provide additional field information if your dictionary contains arrays for each of the elements. 
In this case, you specify the element you want to use like this:
MyString = ‘Hello {0[names][0]}‘
. The resulting 
format()
method input is
MyString.format({‘names‘:[‘George‘, ‘Amy‘]})
. The advantage of this method
is that the input arguments can appear in any order.


MyString = ‘The paths are {0.path}‘
: Provides a means of accessing an attribute 
within an object. The corresponding 
format()
method input is 
MyString.format(sys)
.
If you want to access a specific path, simply include the element specifier like this:
MyString = ‘The path is {0.path[0]}‘
. The advantage of this technique is that you
can access properties within objects without first placing the property value in a variable.
A formatting string can contain as many variables as needed to provide complete information to the 
user. For example, you can add a second argument like this.
MyString = ‘Hello {0} from {1}‘
When you call the 
format()
method, you now need to add some more information. The 
format()
method input for this string might look like this.
MyString.format(‘George’, ‘London’)
548592c05.indd 83
2/24/10 1:21:47 PM
www.finebook.ir


84

CHAPTER 5
InteractIng wIth StructureS and ObjectS
In many cases, you need to provide input that doesn’t translate into a string. For example, you 
might need to provide integer input for some strings. The interpreter won’t automatically perform 
a conversion in this case so you need to perform the task manually. The conversion symbol is the 
exclamation mark (
!
) and the most common conversion is string (
s
). You can also call the 
repr()
conversion function by using 
r
in place of 
s
. Here’s an example of a conversion:
MyString = ‘{0!s} + {1!s} = {2!s}‘
MyString.format(1, 2, 1+2)
In this case, you get an output of ‘1 + 2 = 3’. Notice that this example places the math directly in the 
format string. You could place the output of a function there as well.
So far, the examples haven’t done much formatting — they have simply replaced field values with infor-
mation found in other sources. The format operator is the colon (
:
) and you can combine it with the 
conversion operator if you want. To see how this works, think about displaying the previous example in 
hexadecimal format. In that case, your code might look like this:
MyString = ‘{0:X} + {1:X} = {2:X}‘
MyString.format(10, 20, 10+20)
The output from this code is in hexadecimal format — you’d see ‘A + 14 = 1E’. Of course, you might 
want all the values to take up the same space. In this case, you can tell the interpreter to add some 
space to the output using the following string:
MyString = ‘{0:0=4X} + {1:0=4X} = {2:0=4X}‘
This string outputs numbers with zeros as padding. The padding appears after any sign information. 
In addition, each of the entries is four digits long. Consequently, the output now looks like this: ‘000A 
+ 0014 = 001E’. The formatting has specific entries, all of which are options. It looks like this:
[[fill]align][sign][#][0][width][.precision][type]
Fill characters determine what appears as part of the padding the interpreter uses when you specify 
a width, and the field value doesn’t fill the entire space. The default padding is the space, but you 
can specify any character other than the closing brace, which would end the formatting definition. 
When you specify a fill character, such as the 0 used in the previous example, you must also use one 
of the alignment characters found in Table 5-1.

Download 20,35 Mb.

Do'stlaringiz bilan baham:
1   ...   102   103   104   105   106   107   108   109   ...   453




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