Tutorials :: python tutorials
<
from enum import Enum
class Season(Enum):
Summer = 0
Fall = 1
Winter = 2
Spring = 3
class Status(Enum):
Busy = "Busy"
Available = "Available"
class Switch(Enum):
On = True
Off = False
winter = Season.Winter.value
# winter is 2
available = Status.Available.value
# available is "Available"
on = Switch.On.value
# on is True
he following sections discuss the various elements of the conversion specifiers in more detail.
Simple Conversion
The simple conversion, with only a conversion type, is really easy to use:
>>> 'Price of eggs: $%d' % 42
'Price of eggs: $42'
>>> 'Hexadecimal price of eggs: %x' % 42
'Hexadecimal price of eggs: 2a'
>>> from math import pi
>>> 'Pi: %f...' % pi
'Pi: 3.141593...'
>>> 'Very inexact estimate of pi: %i' % pi
'Very inexact estimate of pi: 3'
>>> 'Using str: %s' % 42L
'Using str: 42'
>>> 'Using repr: %r' % 42L
'Using repr: 42L'
Width and Precision
A conversion specifier may include a field width and a precision. The width is the minimum
number of characters reserved for a formatted value. The precision is (for a numeric conversion) the number of decimals that will be included in the result or (for a string conversion) the
maximum number of characters the formatted value may have.
These two parameters are supplied as two integer numbers (width first, then precision),
separated by a . (dot). Both are optional, but if you want to supply only the precision, you must
also include the dot:
>>> '%10f' % pi # Field width 10
' 3.141593'
class Season(Enum):
Summer = 0
Fall = 1
Winter = 2
Spring = 3
class Status(Enum):
Busy = "Busy"
Available = "Available"
class Switch(Enum):
On = True
Off = False
winter = Season.Winter.value
# winter is 2
available = Status.Available.value
# available is "Available"
on = Switch.On.value
# on is True
he following sections discuss the various elements of the conversion specifiers in more detail.
Simple Conversion
The simple conversion, with only a conversion type, is really easy to use:
>>> 'Price of eggs: $%d' % 42
'Price of eggs: $42'
>>> 'Hexadecimal price of eggs: %x' % 42
'Hexadecimal price of eggs: 2a'
>>> from math import pi
>>> 'Pi: %f...' % pi
'Pi: 3.141593...'
>>> 'Very inexact estimate of pi: %i' % pi
'Very inexact estimate of pi: 3'
>>> 'Using str: %s' % 42L
'Using str: 42'
>>> 'Using repr: %r' % 42L
'Using repr: 42L'
Width and Precision
A conversion specifier may include a field width and a precision. The width is the minimum
number of characters reserved for a formatted value. The precision is (for a numeric conversion) the number of decimals that will be included in the result or (for a string conversion) the
maximum number of characters the formatted value may have.
These two parameters are supplied as two integer numbers (width first, then precision),
separated by a . (dot). Both are optional, but if you want to supply only the precision, you must
also include the dot:
>>> '%10f' % pi # Field width 10
' 3.141593'
No comments