5

I occasionally use Python string formatting. This can be done like so:

print('int: %i. Float: %f. String: %s' % (54, 34.434, 'some text'))

But, this can also be done like this:

print('int: %r. Float: %r. String: %r' % (54, 34.434, 'some text'))

As well as using %s:

print('int: %s. Float: %s. String: %s' % (54, 34.434, 'some text'))

My question is therefore: why would I ever use anything else than the %r or %s? The other options (%i, %f and %s) simply seem useless to me, so I'm just wondering why anybody would every use them?

[edit] Added the example with %s

kramer65
  • 50,427
  • 120
  • 308
  • 488
  • 2
    [When to use %r instead of %s in Python?](http://stackoverflow.com/questions/6005159/when-to-use-r-instead-of-s-in-python) – Grijesh Chauhan Jun 13 '13 at 07:49
  • You shouldn't be using `%` string formatting at all. It has been deprecated in favor of the new [`str.format()` method](http://docs.python.org/2/library/stdtypes.html#str.format); see also [format string syntax](http://docs.python.org/2/library/string.html#formatstrings). – Tim Pietzcker Jun 13 '13 at 08:00
  • For example, for string values the results are different. For `s = 'hello\tworld'`, `%r` gives exactly that including the quote characters. but `%s` would give something like `hello world`. – martineau Jun 13 '13 at 08:02
  • 3
    @TimPietzcker it *was* deprecated but that must've been reversed because there are no current plans to remove it – jamylak Jun 13 '13 at 08:04
  • @TimPietzcker: What jamylak says is true, it's been undeprecated and there's nothing discouraging it use in the [current documentation](http://docs.python.org/2/library/stdtypes.html#string-formatting-operations). – martineau Jun 13 '13 at 08:17
  • I saw performance comparsions between `%` formats and `str.format()` formats, and guess which won? Than may explain the undeprecation :-) – Michał Fita Jun 13 '13 at 08:19
  • I think TimPietzcker has a point though. Although there is nothing discouraging in the 2.7 docs, http://docs.python.org/3.4/library/string.html#string-formatting does talk about 'old style' and 'new style' formatting. I think I'll start trying to get used to the new style formatting from now on then. I am still mainly using Py2.7, but I do try to force myself to getting used to python() as a function as well.. :) – kramer65 Jun 13 '13 at 08:27
  • 1
    @kramer65 I recommend new formatting of course, I was just stating that it's still supported – jamylak Jun 13 '13 at 09:01
  • @jamylak - Ah, like that. Thanks for the help and your answer topoint me to the new way of doing things! – kramer65 Jun 13 '13 at 11:45

4 Answers4

9

For floats, the value of repr and str can vary:

>>> num = .2 + .1
>>> 'Float: %f. Repr: %r Str: %s' % (num, num, num)
'Float: 0.300000. Repr: 0.30000000000000004 Str: 0.3'

Using %r for strings will result in quotes around it:

>>> 'Repr:%r Str:%s' % ('foo','foo')
"Repr:'foo' Str:foo"

You should always use %f for floats and %d for integers.

Rufflewind
  • 8,545
  • 2
  • 35
  • 55
Ashwini Chaudhary
  • 244,495
  • 58
  • 464
  • 504
  • Alright, I forgot about %s. Just added it to my initial question. From the example you give I would always prefer the representation by %s. So as I understand it now, I would only use for example %f if I would want some extra formatting in it (like your %.3f example), right? – kramer65 Jun 13 '13 at 07:58
  • @kramer65 Yes you should always use `%f` for floats, `%s` can work for integers. – Ashwini Chaudhary Jun 13 '13 at 08:03
4

@AshwiniChaudhary answered your question concerning old string formatting, if you were to use new string formatting you would do the following:

>>> 'Float: {0:f}. Repr: {0!r} Str: {0!s}'.format(.2 + .1)
'Float: 0.300000. Repr: 0.30000000000000004 Str: 0.3'

Actually !s is the default so you don't need it, the last format can simply be {0}

jamylak
  • 128,818
  • 30
  • 231
  • 230
2

With the others, you'll have much finer control over your results.

In the docs, they tell you exactly about the way you can fine-tune your results, such as

>>> "%x-%5x-%#5x-%05x-%#05x" % (12,12,12,12,12)
'c-    c-  0xc-0000c-0x00c'
glglgl
  • 89,107
  • 13
  • 149
  • 217
0

You may use the fact that '%f' % variable will raise an exception if the variable is not numeric.

Pawel Furmaniak
  • 4,648
  • 3
  • 29
  • 33