3 ms·
I recently discovered that Python supports four different kinds of string literal (plus two kinds of `bytes` literal). Strings can have no prefix, or be prefixe
by pansa2 7y ago
I recently discovered that Python supports four different kinds of string literal (plus two kinds of `bytes` literal). Strings can have no prefix, or be prefixed with `f`, `r` or `fr`. This proposal simply changes the prefixes to `p`, none, `pr` and `r` respectively.
Do we really need all four types? In particular, I expect use of the last type to be very rare.
Isn't it common in other languages to only have the second and third types? That is, they allow you to choose between using both escape sequences and variable interpolation, or using neither - there's no way to get one without the other?
- j0057 7y agoAnd don't forget we can do string formatting four ways now! >>> a = 42 >>> b = 'bar' >>> 'foo %03d %r' % (a, b) "foo 042 'bar'" >>> 'foo {:03d} {!r}'.format(a, b) "foo 042 'bar'" >>> f"foo {a:03} {b!r}" "foo 042 'bar'" >>> __import__('string').Template('foo $a $b').substitute(locals()) 'foo 42 bar'