{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h1 align=\"center\">Format</h1>"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"print('|{}|{}|{}|'.format('ABC', 1, 1.))\n",
"print('|{2}|{1}|{0}|{1}|{2}|'.format('ABC', 1, 1.))\n",
"print('|{v2}|{0}|{v1}|'.format('ABC', v1 = 1, v2 = 1.))\n",
"print('|{1[v11]}|{1[v10]}|{0}|'.format('ABC', {'v10': 1, 'v11': 1.}))\n",
"print('|{v1[v11]}|{v1[v10]}|{0[0]}|'.format('ABC', v1 = {'v10': 1, 'v11': 1.}))\n",
"print('|{1[1]}|{1[0]}|{0[1]}|'.format('ABC', [1, 1.]))\n",
"print('|{v1[1]}|{v1[0]}|{0[2]}|'.format('ABC', v1 = [1, 1.]))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The general form of a *standard format specifier* is\n",
"\n",
" [[fill]align][sign][#][0][width][,][.precision][type]\n",
"\n",
"where:\n",
"* fill is any character,\n",
"* align is \"<\", \">\", \"=\" or \"^\"\n",
"* sign is \"+\", \"-\" or \" \"\n",
"* width and precision are integers\n",
"* type is \"b\", \"c\", \"d\", \"e\", \"E\", \"f\", \"F\", \"g\", \"G\", \"n\", \"o\", \"s\", \"x\", \"X\" or \"%\""
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"# Can be replaced by:\n",
"# print('|{:9s}|{:<9s}|{:>9s}|{:^9s}|'.format('ABCDE', 'ABCDE', 'ABCDE', 'ABCDE', 'ABCDE'))\n",
"print('|{:9}|{:<9}|{:>9}|{:^9}|'.format('ABCDE', 'ABCDE', 'ABCDE', 'ABCDE'))\n",
"\n",
"# Can be replaced by:\n",
"# print('|{:9s}|{:_<9s}|{:_>9s}|{:_^9s}|'.format('', 'ABCDE', 'ABCDE', 'ABCDE', 'ABCDE'))\n",
"print('|{:9}|{:_<9}|{:_>9}|{:_^9}|'.format('', 'ABCDE', 'ABCDE', 'ABCDE'))\n",
"\n",
"# Can be replaced by:\n",
"# print('|{:.5s}|'.format('ABCDEFGHIJ'))\n",
"print('|{:.5}|'.format('ABCDEFGHIJ'))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"print('|{:c}|'.format(9731))\n",
"print('|{:9c}|{:<9c}|{:>9c}|{:^9c}|'.format(9731, 9731, 9731, 9731))\n",
"print('|{:09c}|{:_<9c}|{:_>9c}|{:_^9c}|'.format(9731, 9731, 9731, 9731))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"# Can be replaced by either:\n",
"# print('|{:8d}|{:<8d}|{:>8d}|{:=8d}|{:^8d}|'.format(-123, -123, -123, -123, -123))\n",
"# or:\n",
"# print('|{:8n}|{:<8n}|{:>8n}|{:=8n}|{:^8n}|'.format(-123, -123, -123, -123, -123))\n",
"print('|{:8}|{:<8}|{:>8}|{:=8}|{:^8}|'.format(-123, -123, -123, -123, -123))\n",
"\n",
"# Can be replaced by either:\n",
"# print('|{:08d}|{:_<8d}|{:_>8d}|{:_=8d}|{:_^8d}|'.format(-123, -123, -123, -123, -123))\n",
"# or:\n",
"# print('|{:08n}|{:_<8n}|{:_>8n}|{:_=8n}|{:_^8n}|'.format(-123, -123, -123, -123, -123))\n",
"print('|{:08}|{:_<8}|{:_>8}|{:_=8}|{:_^8}|'.format(-123, -123, -123, -123, -123))\n",
"\n",
"# Can be replaced by:\n",
"# print('{:d}|{:+d}|{:-d}|{: d}|'.format(+123, 123, 123, 123))\n",
"# or:\n",
"# print('{:n}|{:+n}|{:-n}|{: n}|'.format(+123, 123, 123, 123))\n",
"print('|{}|{:+}|{:-}|{: }|'.format(+123, 123, 123, 123))\n",
"\n",
"# Can be replaced by either:\n",
"# print('|{:,d}|'.format(-12345678))\n",
"# or:\n",
"# print('|{:,n}|'.format(-12345678))\n",
"print('|{:,}|'.format(-12345678))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"print('|{:12b}|{:<12b}|{:>12b}|{:=12b}|{:^12b}|'.format(-37, -37, -37, -37, -37))\n",
"print('|{:012b}|{:_<12b}|{:_>12b}|{:_=12b}|{:_^12b}|'.format(-37, -37, -37, -37, -37))\n",
"print('|{:b}|{:+b}|{:-b}|{: b}|'.format(+37, 37, 37, 37))\n",
"print()\n",
"\n",
"print('|{:#12b}|{:<#12b}|{:>#12b}|{:=#12b}|{:^#12b}|'.format(-37, -37, -37, -37, -37))\n",
"print('|{:#012b}|{:_<#12b}|{:_>#12b}|{:_=#12b}|{:_^#12b}|'.format(-37, -37, -37, -37, -37))\n",
"print('|{:#b}|{:+#b}|{:-#b}|{: #b}|'.format(37, 37, 37, 37))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"print('|{:9o}|{:<9o}|{:>9o}|{:=9o}|{:^9o}|'.format(-69, -69, -69, -69, -69))\n",
"print('|{:09o}|{:_<9o}|{:_>9o}|{:_=9o}|{:_^9o}|'.format(-69, -69, -69, -69, -69))\n",
"print('|{:o}|{:+o}|{:-o}|{: o}|'.format(+69, 69, 69, 69))\n",
"print()\n",
"\n",
"print('|{:#9o}|{:<#9o}|{:>#9o}|{:=#9o}|{:^#9o}|'.format(-69, -69, -69, -69, -69))\n",
"print('|{:#09o}|{:_<#9o}|{:_>#9o}|{:_=#9o}|{:_^#9o}|'.format(-69, -69, -69, -69, -69))\n",
"print('|{:#o}|{:+#o}|{:-#o}|{: #o}|'.format(69, 69, 69, 69))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"print('|{:9x}|{:<9x}|{:>9x}|{:=9x}|{:^9x}|'.format(-27, -27, -27, -27, -27))\n",
"print('|{:09x}|{:_<9x}|{:_>9x}|{:_=9x}|{:_^9x}|'.format(-27, -27, -27, -27, -27))\n",
"print('|{:x}|{:+x}|{:-x}|{: x}|'.format(+27, 27, 27, 27))\n",
"print()\n",
"\n",
"print('|{:#9x}|{:<#9x}|{:>#9x}|{:=#9x}|{:^#9x}|'.format(-27, -27, -27, -27, -27))\n",
"print('|{:#09x}|{:_<#9x}|{:_>#9x}|{:_=#9x}|{:_^#9x}|'.format(-27, -27, -27, -27, -27))\n",
"print('|{:#x}|{:+#x}|{:-#x}|{: #x}|'.format(27, 27, 27, 27))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"print('|{:9X}|{:<9X}|{:>9X}|{:=9X}|{:^9X}|'.format(-27, -27, -27, -27, -27))\n",
"print('|{:09X}|{:_<9X}|{:_>9X}|{:_=9X}|{:_^9X}|'.format(-27, -27, -27, -27, -27))\n",
"print('|{:X}|{:+X}|{:-X}|{: X}|'.format(+27, 27, 27, 27))\n",
"print()\n",
"\n",
"print('|{:#9X}|{:<#9X}|{:>#9X}|{:=#9X}|{:^#9X}|'.format(-27, -27, -27, -27, -27))\n",
"print('|{:#09X}|{:_<#9X}|{:_>#9X}|{:_=#9X}|{:_^#9X}|'.format(-27, -27, -27, -27, -27))\n",
"print('|{:#X}|{:+#X}|{:-#X}|{: #X}|'.format(27, 27, 27, 27))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"print('|{:10}|{:<25}|{:>25}|{:=25}|{:^25}|'.format(\n",
" -1234., -123456789012345.6789, -1234567890123456.789,\n",
" -12345678901234567.89, -123456789012345678.9))\n",
"print('|{:010}|{:_<25}|{:_>25}|{:_=25}|{:_^25}|'.format(\n",
" -1234., -123456789012345.6789, -1234567890123456.789,\n",
" -12345678901234567.89, -123456789012345678.9))\n",
"print()\n",
"\n",
"print('|{}|{:+}|{:-}|{: }|'.format(+123456789012345.6789, 1234567890123456.789,\n",
" 12345678901234567.89, 123456789012345678.9))\n",
"print('|{:,}|'.format(-12345.6789))\n",
"print('|{:.5}|{:.5}|'.format(-12.3456789, -123456789012345678.9))\n",
"print('|{:.0}|{:#.0}|'.format(12.34, 12.34))\n",
"print()\n",
"\n",
"print('{:} {:}'.format(float('inf'), float('Nan')))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"# Can be replaced by:\n",
"# print('|{:15F}|{:<15F}|{:>15F}|{:=15F}|{:^15F}|'.format(\n",
"# -12.3456789, -12.3456789, -12.3456789, -12.3456789, -12.3456789))\n",
"print('|{:15f}|{:<15f}|{:>15f}|{:=15f}|{:^15f}|'.format(\n",
" -12.3456789, -12.3456789, -12.3456789, -12.3456789, -12.3456789))\n",
"# Can be replaced by:\n",
"# print('|{:015F}|{:_<15F}|{:_>15F}|{:_=15F}|{:_^15F}|'.format(\n",
"# -12.3456789, -12.3456789, -12.3456789, -12.3456789, -12.3456789))\n",
"print('|{:015f}|{:_<15f}|{:_>15f}|{:_=15f}|{:_^15f}|'.format(\n",
" -12.3456789, -12.3456789, -12.3456789, -12.3456789, -12.3456789))\n",
"print()\n",
"\n",
"# Can be replaced by:\n",
"# print('|{:F}|{:+F}|{:-F}|{: F}|'.format(+123456789., 123456789., 123456789., 123456789.))\n",
"print('|{:f}|{:+f}|{:-f}|{: f}|'.format(+123456789., 123456789., 123456789., 123456789.))\n",
"# Can be replaced by:\n",
"# print('|{:,F}|'.format(-12345.6789))\n",
"print('|{:,f}|'.format(-12345.6789))\n",
"# Can be replaced by:\n",
"# print('|{:.5F}|'.format(-12.3456789))\n",
"print('|{:.5f}|'.format(-12.3456789))\n",
"# Can be replaced by:\n",
"# print('|{:.0F}|{:#.0F}|'.format(12.34, 12.34))\n",
"print('|{:.0f}|{:#.0f}|'.format(12.34, 12.34))\n",
"print()\n",
"\n",
"print('{:f} {:f}'.format(float('inf'), float('Nan')))\n",
"print('{:F} {:F}'.format(float('inf'), float('Nan')))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"print('|{:15e}|{:<15e}|{:>15e}|{:=15e}|{:^15e}|'.format(\n",
" -12.3456789, -12.3456789, -12.3456789, -12.3456789, -12.3456789))\n",
"print('|{:15E}|{:<15E}|{:>15E}|{:=15E}|{:^15E}|'.format(\n",
" -12.3456789, -12.3456789, -12.3456789, -12.3456789, -12.3456789))\n",
"print()\n",
"\n",
"print('|{:015e}|{:_<15e}|{:_>15e}|{:_=15e}|{:_^15e}|'.format(\n",
" -12.3456789, -12.3456789, -12.3456789, -12.3456789, -12.3456789))\n",
"print('|{:015E}|{:_<15E}|{:_>15E}|{:_=15E}|{:_^15E}|'.format(\n",
" -12.3456789, -12.3456789, -12.3456789, -12.3456789, -12.3456789))\n",
"print()\n",
"\n",
"print('|{:e}|{:+e}|{:-e}|{: e}|'.format(+12.3456789, 12.3456789, 12.3456789, 12.3456789))\n",
"print('|{:E}|{:+E}|{:-E}|{: E}|'.format(+12.3456789, 12.3456789, 12.3456789, 12.3456789))\n",
"print()\n",
"\n",
"print('|{:.5e}|'.format(-12.3456789))\n",
"print('|{:.5E}|'.format(-12.3456789))\n",
"print()\n",
"\n",
"print('|{:.0e}|{:#.0e}|'.format(12.34, 12.34))\n",
"print('|{:.0E}|{:#.0E}|'.format(12.34, 12.34))\n",
"print()\n",
"\n",
"print('{:e} {:e}'.format(float('inf'), float('Nan')))\n",
"print('{:E} {:E}'.format(float('inf'), float('Nan')))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"print('|{:15g}|{:<15g}|{:>15g}|{:=15g}|{:^15g}|'.format(\n",
" -1234., -12345.6789, -123456.789, -1234567.89, -12345678.9))\n",
"print('|{:15G}|{:<15G}|{:>15G}|{:=15G}|{:^15G}|'.format(\n",
" -1234., -12345.6789, -123456.789, -1234567.89, -12345678.9))\n",
"print()\n",
"\n",
"print('|{:015g}|{:_<15g}|{:_>15g}|{:_=15g}|{:_^15g}|'.format(\n",
" -1234.56789, -12345.6789, -123456.789, -1234567.89, -12345678.9))\n",
"print('|{:015G}|{:_<15G}|{:_>15G}|{:_=15G}|{:_^15G}|'.format(\n",
" -1234.56789, -12345.6789, -123456.789, -1234567.89, -12345678.9))\n",
"print()\n",
"\n",
"print('|{:g}|{:+g}|{:-g}|{: g}|'.format(+1234.56789, 12345.6789, 123456.789, 1234567.89))\n",
"print('|{:G}|{:+G}|{:-G}|{: G}|'.format(+1234.56789, 12345.6789, 123456.789, 1234567.89))\n",
"print()\n",
"\n",
"print('|{:.5g}|{:.5g}|'.format(-12.3456789, -1234567.89))\n",
"print('|{:.5G}|{:.5G}|'.format(-1234567.89, -1234567.89))\n",
"print()\n",
"\n",
"print('|{:g}|{:#g}|{:g}|{:#g}|'.format(12., 12., 10000000, 10000000))\n",
"print('|{:G}|{:#G}|{:G}|{:#G}|'.format(12., 12., 10000000, 10000000))\n",
"print()\n",
"\n",
"print('{:g} {:g}'.format(float('inf'), float('Nan')))\n",
"print('{:G} {:G}'.format(float('inf'), float('Nan')))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"print('|{:15%}|{:<15%}|{:>15%}|{:=15%}|{:^15%}|'.format(\n",
" -12.3456789, -12.3456789, -12.3456789, -12.3456789, -12.3456789))\n",
"print('|{:015%}|{:_<15%}|{:_>15%}|{:_=15%}|{:_^15%}|'.format(\n",
" -12.3456789, -12.3456789, -12.3456789, -12.3456789, -12.3456789))\n",
"print()\n",
"\n",
"print('|{:%}|{:+%}|{:-%}|{: %}|'.format(+12.345, 12.345, 12.345, 12.345))\n",
"print('|{:,%}|'.format(-12345.6789))\n",
"print('|{:.5%}|'.format(-12.3456789))"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.4.3"
}
},
"nbformat": 4,
"nbformat_minor": 0
}
Resource created Wednesday 19 August 2015, 10:06:44 AM.
file: format.ipynb