User Tools

Site Tools


python:py3format

22.05.2009

Requires Python 3.0.

#!/usr/bin/env python3.0
#
#       format_example.py
#       
#       
#format_spec ::=  [[fill]align][sign][#][0][width][.precision][type]
#fill        ::=  <a character other than '}'>
#align       ::=  "<" | ">" | "=" | "^"
#sign        ::=  "+" | "-" | " "
#width       ::=  integer
#precision   ::=  integer
#type        ::=  "b" | "c" | "d" | "e" | "E" | "f" | "F" | "g" | "G" | "n" | "o" | "x" | "X" | "%"
 
#
 
 
def main():
    age = 29
    name = 'John Doe'
 
    print('My name is {0} and my age is {1}'.format(name, age))
    print('Name filled up to 10 char with spaces ->{0:10}<-'.format(name))
    print('Name filled up to 20 char with *; left aligned ->{0:*<20}<-'.format(name))
 
    numberFloat = 1243.2346
    print('Number {1} with 3 digits precision: {0:.3f}'.format(numberFloat, numberFloat))
 
if __name__ == '__main__': main()

python/py3format.txt · Last modified: 2013/03/16 17:41 (external edit)