String Formatting

C-Style Formatting (1)

Good old C: %[flags][width][.precision]type}

Good ol’ C

Python

Program
int i = 42;
float f = 3.14159265359;
printf("%07d, %8.4f\n", i, f);
Output
0000042,   3.1416
  • Same in Python, using the % operator (humorously)

  • Format single item into string, by position

'%07d' % 42
'0000042'
  • Format multiple items (⟶ tuple) into string (again, by position)

'%07d, %8.4f' % (42, 3.14159265359)
'0000042,   3.1416'

C-Style Formatting: Conversions

Frequently used conversions

Format char

Type

Comment

s

String

c

Single character

d

Integer

decimal

o

Integer

octal

x

Integer

hexadecimal lowercase

X

Integer

hexadecimal uppercase

f

Floating point

exponential format (lowercase)

F

Floating point

exponential format (uppercase)

\%

The % sign itself

C-Style Formatting: Flags

Frequently used flags

Flag

Meaning

#

Octal or hex integer conversions: 0x... prefixes

0

Pad with ‘0’ characters

-

Left alignment

+

Print sign even if positive

`` `` (space)

Print space in place of sign if positive

C-Style Formatting: Examples

'%#5X' % 47
' 0X2F'
'%5X' % 47
'   2F'
'%#5.4X' % 47
'0X002F'
'%#5o' % 25
' 0o31'
'%+d' % 42
'+42'
'% d' % 42
' 42'
'%+2d' % 42
'+42'
'% 4d' % 42
'  42'
'% 4d' % -42
' -42'
'%04d' % 42
'0042'

The format Method

Problems with C-style formatting

  • Not flexible enough (as always)

  • Positional parameters only

  • Parameter position must match occurence in format string

A better (?) way of formatting
>>> '0 {0:05d}, 1 {1:8.2f}, 0 again {0}'.format(42, 1.5)
'0 00042, 1     1.50, 0 again 42'
>>> 'a {a:05d}, b {b:8.2f}, a again {a}'.format(a=42, b=1.5)
'a 00042, b     1.50, a again 42'

Cool Since 3.6: f-Strings

  • Definitely cool

  • In-string syntax like above, but reaching out …

something = 42
f'Hey, something has a value, namely {something}'
'Hey, something has a value, namely 42'
  • All of the fancy C-style format syntax is valid, e.g.

something = 42.666
f'Well, this is also possible: {something:8.2f}'
'Well, this is also possible:    42.67'