11
Use "%" or ".format"? I think the last one is newer, but there is some other aspect to be taken into account when deciding which one to use?
11
Use "%" or ".format"? I think the last one is newer, but there is some other aspect to be taken into account when deciding which one to use?
13
For 3.6+ versions, use f-strings for interpolation: How to interpolate string in Python?
For versions 3+, prior to 3.6, always use the method format
, it was set precisely to replace the %
- for version 3.6 or higher, read Formatted string literals. The PEP that suggested such a change was PEP 3101:
PEP 3101 -- Advanced String Formatting
And some of the points that led to such a change are:
The operator %
is a binary operator, so you will always receive two parameters. The first one is already reserved for string which will be formatted, then the language is limited to passing all the formatting values through the second parameter;
This has some implications:
The parameter that has the formatting values will necessarily be a composite type, as it must have the ability to store multiple values;
>>> string % parameters
It is limited to always using only positional parameters, when passing them through a tuple, or named parameters, when passing them through a dictionary, but never both concurrently;
>>> '%s %s' % ('john', 'doe') # posicional
john doe
>>> '%(first)s %(last)s' % {'first': 'john', 'last': 'doe'} # nomeado
john doe
Impairs semantics as a binary operator is not expected to format a string;
>>> 5 % 2 # Calcula o resto da divisão
1
>>> string % parameters # Formata string????
Unable to use language tools such as tuple deconstructing or Dict deconstructing to facilitate the passage of values;
>>> name = ['john', 'doe']
>>> '%s %s' % (*name)
SyntaxError: invalid syntax
Other features were added to the new style when using the method format
:
Use positional parameters, but use them in drawn order;
>>> '{1}, {0}'.format('john', 'doe')
doe, john
Define spacing in string replacing the character to be displayed: in the old style, a blank space is always displayed;
>>> '{:_<10}'.format('john')
john______
Center content with reference to available space;
>>> '{:_^10}'.format('john')
+++john+++
When using signposted numbers, it is possible to control the signal position;
>>> '{:=5d}'.format(-3)
- 3
>>> '{:=+5d}'.format(3)
+ 3
The method format
accepts parameters named to define values, no longer depending on dictionaries or tuples;
>>> '{first} {last}'.format(first='john', last='doe')
john doe
What makes it possible to use tuple deconstructing or Dict deconstructing:
>>> name = ('john', 'doe')
>>> '{0} {1}'.format(*name)
john doe
>>> name = {'first': 'john', 'last': 'doe'}
>>> '{first} {last}'.format(**name)
john doe
You can access values directly from string;
>>> john = {'first': 'john', 'last': 'doe'}
>>> '{name[first]} {name[last]}'.format(name=john)
john doe
Similarly, it is possible to access object attributes;
>>> 'Nome do arquivo: {0.name}'.format(open('arquivo.txt'))
Nome do arquivo: arquivo.txt
It is possible for objects to take control of their own formatting, just as with the object datetime.datetime
;
>>> from datetime import datetime
>>> '{:%Y-%m-%d %H:%M}'.format(datetime.now())
2017-12-11 20:52
This is possible because the method format
will seek by the method __format__
of the object;
It is possible to parameterize the format itself with its values;
>>> '{:_{align}{width}}'.format('john', align='^', width='10')
___john___
>>> '{:_{align}{width}}'.format('john', align='>', width='10')
______john
Other references
Browser other questions tagged python
You are not signed in. Login or sign up in order to post.
There’s an entire PEP to consider: PEP 3101 -- Advanced String Formatting
– Woss
@AndersonCarlosWoss o https://dbader.org/blog/python-string-formatting
– CBredlow