User Tools

Site Tools


python:py3format

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

python:py3format [2013/03/16 17:41] (current)
Line 1: Line 1:
 +=== PRINT AND FORMAT EXAMPLE ===
 +22.05.2009
  
 +
 +Requires Python 3.0.
 +
 +<code python>
 +#​!/​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()
 +
 +</​code>​
python/py3format.txt ยท Last modified: 2013/03/16 17:41 (external edit)