• No results found

Formatting—Version I

In document Programming with Python [2017].pdf (Page 187-190)

Operations with Files

8.2 String Formatting

8.2.1 Formatting—Version I

Details of formatting in thefirst version are summarized in Fig. 8.3. The scheme is characterized by the following:

1. The string has as many replacement fields as the number of entities in the tuple. The replacementfields appear in the same order as the entities in the tuple.

2. The modulo operator‘%’ signifies the start of each replacement field.

3. The ‘%’ character is followed by four optional components and a final mandatory character signifying the type of conversion to be carried out. This conversion character can be one from Table8.2.

4. Thefirst optional component is a mapping key. It is present only if the items to be put in the replacementfields are specified through a dictionary instead of a tuple.

5. An optional flag modifies the structure/orientation of the entity. The possible flag types and their effects are given in Table 8.3.

6. An integer specifying the width/number of spaces to be allocated in the string to the entity forms the next component.

Formatted string % Source

dictionary {‘k1’:a1,‘k2’:a2,. . . . ‘kk’:ak}

Tuple ‘a1, a2, . . . . ak’

Single string ‘aa’

Signifies start of source

‘xx. . ..xx%F1xx. . ..xx%F2xx. . . . ..xx%Fkxx….xx’

Literal character sets

Fields to be replaced from source – as many numbers as elements in source

Fi % ( ) F W . p f

Conversion type (See table 8.2)

Precision: No. of digits after decimal point (optional – only if the element is a number) Min. field width as integer

Conversion flag (optional – See Table 8.3) Mapping key – present only if source is a dictionary Signifies start of specifier

ith field: number of fields same as number of elements in in source – replaced in same order as in source

Fig. 8.3 Details of string formation for printing—Version I

7. A dot—‘.’—followed by an integer signifying the desired precision in the presentation is the next component. This and the two previous components implicitly assume the tuple entity to be a number.

8. A modulus character—‘%’—at the end of the string being formed precedes the object to be formatted.

9. In case the formatting is for a single item, it can be present as such. There is no need of presenting it as a (single element) tuple.

10. If the field width/precision is specified through an asterisk mark—‘*’—the concerned numerical value has to be available as an integer in the tuple;

position-wise it is prior to the entity concerned.

A number of formatting examples is given in the illustrative Python Interpreter sequence in Fig.8.4.aa is assigned the numerical value of −3.21 in [1]. Being a Table 8.2 Conversion characters for the first version of string formatting

Conversion Meaning

‘d’ Signed integer decimal

‘i’ Signed integer decimal

‘o’ Signed octal value

‘x’ Signed hexadecimal (lowercase)

‘X’ Signed hexadecimal (uppercase)

‘e’ Floating point exponential format (lowercase)

‘E’ Floating point exponential format (uppercase)

‘f’ Floating point decimal format

‘F’ Floating point decimal format

‘g’ Floating point format—uses lowercase exponential format if exponent is less than 4 or not less than precision, decimal format otherwise

‘G’ Floating point format—uses uppercase exponential format if exponent is less than 4 or not less than precision, decimal format otherwise

‘c’ Single character (accepts integer or single character string)

‘r’ String (converts any Python object using repr())

‘s’ String (converts any Python object using str())

‘a’ String (converts any Python object using ascii())

‘%’ No argument is converted, results in a‘%’character in the result

Table 8.3 Conversion flags for the first version of string formatting Flag Meaning

‘#’ Value conversion will use alternate form

‘0’ Conversion will be zero padded for numeric values

‘−’ Converted value is left adjusted (overrides the‘0’ conversion if both are given)

‘’ (a space) A blank should be left before a positive number (or empty string) produced by a signed conversion

‘+’ A sign character (‘+’ or ‘−’) will precede the conversion (overrides a “space” flag)

180 8 Operations with Files

single item it is directlyfitted into the string and output [2]. The width specified as six is for the whole number representation—inclusive of the sign and the decimal point.az in [3] is a tuple of numbers selected to bring out the flexibility possible in

>>> aa = -3.21 [1]

>>> print('n=%6.2f'% aa) [2]

n= -3.21

>>> az = (3, -3.2104, -321.04, 321.04, -3210.401) [3]

>>> while True:

>>> rm = {'Name':'Roshan', 'Subject': 'Maths', 'Marks':

100} [9]

>>> print('%(Name)s gets in %(Subject) s %(Marks)4d out of 100' %rm) [10]

Roshan gets in Maths 100 out of 100 [10a]

>>> ab = 91 [11]

>>> while True:

... print('Hex value of ab is %8x'%ab) [12]

... print('Explicit Hex value of ab is %#8x'%ab) [13]

... print('Octal value of ab is %8o'%ab) [14]

... print('Explicit Octal value of ab is %#8o'%ab) [15]

... break

>>> z0 = 42.109 [18]

>>> '%*.2f'%(8,z0) [19]

Fig. 8.4 Python Interpreter sequence illustrating string formatting conforming to Version 1

formatting. The numbers are printed out to different format specifications in the following lines. Theflag—‘#’—in [4] demands output in decimal form [4a] even though the number (az[0] = 3) is an integer. With proper formatting specified the numbers for [4], [5], and [6] are output [4a], [5a], and [6a] properly aligned. The same is not true of the next two outputs. Theflag—‘0’—in [5] and [5a] ensures 0 padding to the left of the number in lieu of blank spaces. The blank space used as a flag in [6] is a space provision for a sign (as ‘−’ if the number is negative). The flag

—‘−’—in [7] and [7a] results in left adjusted output. The output uses the floating point exponent in [8] and [8a] due to the use of the conversion character—‘e’. rm in [9] is a dictionary of three entries—two strings followed by an integer. The string—‘Roshan gets in Maths 100 out of 100’ [10a]—is formed from it and output. The keys—‘Name’ and ‘subject’—are replaced by the respective string s (‘Rohan’ and ‘Maths’) and the third key—‘Marks’—replaced by the corresponding integer—100. The integer value—91—is assigned to ab in [11]

and output in different formats in succeeding lines—that is hex and octal values with and without respective prefixes (0x and 0O respectively). z1 in [16] is a tuple of two elements—a single character string (‘h’) and an integer (123). With U+123 as the Unicode of a character (‘{’) a string is formed in [17] with the single character format. With‘*’ as the width allocated for the entity in [19] the width value is specified (as 8) in the tuple that follows. Similarly the asterisk in [20]

signifies the precision desired. Its value is specified as two in the tuple. In [21] the width as well as the precision is specified in the tuple itself. In all these cases the assignments to the asterisks have to precede the concerned element value in the same order.

In document Programming with Python [2017].pdf (Page 187-190)