-1

So I understand that you can do, for example:

x = input("Enter an integer: ")
print("I am now printing variable x: {}".format(x))

To get the output of whatever string I put in there, as {} is simply the placeholder for the variable.

However, on a website I saw {:d} being used, and despite experimenting in Python to figure out what it does, I cannot seem to find its use. Why is {:d} used when printing the variable and what use does it have?

Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158
Andrew
  • 19
  • 1
  • 5
  • 2
    Please look at the documentation: https://docs.python.org/3.7/library/string.html It's a format specifier. – Matthieu Brucher Nov 26 '18 at 10:15
  • This is a [Format Specifier](https://www.python.org/dev/peps/pep-3101/#format-specifiers) – Abdul Niyas P M Nov 26 '18 at 10:16
  • For string formatting I prefer to use https://pyformat.info over the normal docs. For the case of `{:d}`, which I agree is very hard to search for online, see [this section](https://pyformat.info/#number) – Bill Cheatham Nov 26 '18 at 10:19

1 Answers1

0

In python you can specify the format inside the curved brackets.

You can do things such as

>>> '{:b}'.format(2)
'10'

In your case, d prints as decimal integer.

You can find all the doc here

https://docs.python.org/2/library/string.html#format-specification-mini-language

BlueSheepToken
  • 5,751
  • 3
  • 17
  • 42