string-Formatting Expression Syntax
Table 7-4. String formatting type codes
Code Meaning- s String (or any object’s str(X) string)
- r Same as s, but uses repr, not str
- c Character (int or str)
- d Decimal (base-10 integer)
- i Integer
- u Same as d (obsolete: no longer unsigned)
- o Octal integer (base 8)
- x Hex integer (base 16)
- X Same as x, but with uppercase letters
- e Floating point with exponent, lowercase
- E Same as e, but uses uppercase letters
- f Floating-point decimal
- F Same as f, but uses uppercase letters
- g Floating-point e or f
- G Floating-point E or F
- % Literal % (coded as %%)
When sizes are not known until runtime, you can use a computed width and precision
by specifying them with a * in the format string to force their values to be taken from
the next item in the inputs to the right of the % operator—the 4 in the tuple here gives
precision:
'%f, %.2f, %.*f' % (1/3.0, 1/3.0, 4, 1/3.0)
'0.333333, 0.33, 0.3333'
Dictionary-Based Formatting Expressions
'%(qty)d more %(food)s' % {'qty': 1, 'food': 'spam'}
'1 more spam'
string-Raw Strings Suppress Escapes
This is just the sort of thing that raw strings are useful for. Ifthe letter r (uppercase or
lowercase) appears just before the opening quote of a string, itturns off the escape
mechanism. The result is that Python retains your backslashesliterally, exactly as you
type them. Therefore, to fix the filename problem, just remember toadd the letter r on
Windows:
myfile = open(r'C:\new\text.dat', 'w')
Extended Sequence Unpacking in Python 3.X
When a starred name is used, the number of items in the target on the left need not
match the length of the subject sequence. In fact, the starred name can appear anywhere
in the target. For instance, in the next interaction b matches the last item in the sequence,
and a matches everything before the last:
>>> a
1
>>> b
[2, 3]
>>> c
4
Truth Values and Boolean Tests
• All objects have an inherent Boolean true or false value.
• Any nonzero number or nonempty object is true.
• Zero numbers, empty objects, and the special object None are considered false.
• Comparisons and equality tests are applied recursively to data structures.
• Comparisons and equality tests return True or False (custom versions of 1 and 0).
• Boolean and and or operators return a true or false operand object.
• Boolean operators stop evaluating (“short circuit”) as soon as a result is known.
if X:
A = Y
else:
A = Z
the same thing in one expression:
A = Y if X else Z
while and for Loops
the while statement, provides a way to code general loops. The
second, the for statement, is designed for stepping through the items in a sequence or
other iterable object and running a block of code for each.
break
Jumps out of the closest enclosing loop (past the entire loop statement)
continue
Jumps to the top of the closest enclosing loop (to the loop’s header line)
pass
Does nothing at all: it’s an empty statement placeholder
Loop else block
Runs if and only if the loop is exited normally (i.e., without hitting a break)
while x: # Exit when x empty
if match(x[0]):
print('Ni')
break # Exit, go around else
x = x[1:]
else:
print('Not found') # Only here if exhausted x