Pure Programmer
Blue Matrix


Cluster Map

Project: Aligning Integers with printf()

Write a program that demonstrates left-padding, right-padding and zero-padding using format() and sprintf() style formatting. Use the vertical bar character before and after the format specifier so that you can see where the output begins and ends. Use printf() to print the 32-bit integers and sprintf() to print the 64-bit integers. Print the values in both decimal and hexadecimal. Your program should reproduce the output shown using the following variables:

  • a32 = 106276
  • b32 = -2147483648
  • c32 = 4080400
  • a64 = 11294588176
  • b64 = -4294967296
  • c64 = 9223372036854775807

Output
$ python3 AligningIntegersWithPrintf.py | 106276| 19f24| |106276 |19f24 | |00000106276|000019f24| |-2147483648|-80000000| |-2147483648|-80000000| |-2147483648|-80000000| | 4080400| 3e4310| |4080400 |3e4310 | |00004080400|0003e4310| | 11294588176| 2a135bd10| |11294588176 |2a135bd10 | |00000000011294588176|000000002a135bd10| | -4294967296| -100000000| |-4294967296 |-100000000 | |-0000000004294967296|-0000000100000000| | 9223372036854775807| 7fffffffffffffff| |9223372036854775807 |7fffffffffffffff | |09223372036854775807|07fffffffffffffff|

Solution