Related: % and str.format () in Python. Which one do you use?
There was a mention of f-strings above, but I feel that it is not the only way to convert to strings. Here are some notes that you can think of.
>>> import datetime
>>> d = datetime.datetime(2017, 9, 1, 12, 12, 0, 0)
>>> str(d)
'2017-09-01 12:12:00'
>>> repr(d)
'datetime.datetime(2017, 9, 1, 12, 12)'
>>> eval(repr(d))
datetime.datetime(2017, 9, 1, 12, 12)
repr (obj)
internally calls ʻobj.repr (), and
str (obj) internally implements ʻobj.__str__ ()
, otherwise ʻobj. repr () `is called and used instead.
The result of repr (obj)
is expected to be a Python expression if possible, but this is not always the case. In the datetime example above, a string that can be evaluated with ʻeval () `is returned.
There is no requirement that the result of str (obj)
should be a Python expression, and it is common to prioritize a human-readable format.
When you say "convert an object into an easy-to-read string", you can think of, for example, the pprint
module. It seems that it uses ʻobj.repr () . Well, it's impossible to grasp the structure from the result of
__str __ ()`.
str.format ()
It was the subject of the above article. I think it is the most important in everyday development, but it is omitted.
f-strings
A notation introduced in Python 3.6 that allows you to write Python expressions directly inside a string.
PEP 498 -- Literal String Interpolation
An example of FizzBuzz.
>>> print(*[f"{'Fizz'*(not i%3)}{'Buzz'*(not i%5)}" or i for i in range(1,31)], sep='\n')
1
2
Fizz
4
Buzz
Fizz
7
8
Fizz
Buzz
11
Fizz
13
14
FizzBuzz
16
17
Fizz
19
Buzz
Fizz
22
23
Fizz
Buzz
26
Fizz
28
29
FizzBuzz
I refer to Reddit's "a Py3.6 fizzBuzz oneliner with f-strings", but map ()
I changed to using list comprehension rather than using list comprehension recently, so I changed it (reduce ()
may have dropped the library).
Just in case, the *
at the beginning of the argument ofprint ()
indicates the unpacking of the list.
In terms of type checking, at least mypy 0.521 looks inside f-strings.
$ mypy --version
mypy 0.521
$ cat /tmp/test.py
def func(a: int) -> int:
return a ** 2
print(f'{func(10.0)}')
$ python /tmp/test.py
100.0
$ mypy /tmp/test.py
/tmp/test.py:4: error: Argument 1 to "func" has incompatible type "float"; expected "int"
Unfortunately, I often use 3.5 or earlier, so I haven't taken the initiative to unify it.
However, in the future, I think there is a possibility that f-strings will become the center, overcoming str.format ()
.
[https://docs.python.jp/3/library/string.html#template-strings](template string)
The old-fashioned feature of template strings is to replace $ variables
, which is built into Python. I will briefly introduce the above sample.
>>> from string import Template
>>> s = Template('$who likes $what')
>>> s.substitute(who='tim', what='kung pao')
'tim likes kung pao'
Unfortunately, HTML escaping etc. are not supported. I rarely see it personally, but I have considered using it.
The title says six, but there are still some standard Python features.
pprint
moduleformat ()
(Commented. I didn't know. Reference)There should be other ways to stringify using an external library such as jinja2. Of course, if you make it yourself, you will have more choices.
Recommended Posts