-3

I'm new to Python and we're currently learning how to use if/elif/else and as a exercise our prof. wants us to write a program that answers if a year is a leap year or not.I found a guide that shows and gives you a pretty good explanation on how to write such a program.

The code looks like this:

year = int(input("Please Enter the Year Number you wish: "))

if (year%400 == 0):
          print("%d is a Leap Year" %year)
elif (year%100 == 0):
          print("%d is Not the Leap Year" %year)
elif (year%4 == 0):
          print("%d is a Leap Year" %year)
else:
          print("%d is Not the Leap Year" %year

The only thing I am trying to figure out but haven't been able to find a good answer to is why the author uses print("%d this is a leap year" %year)

How come %d when the running the program doesn't show up as %d when inside a string?

Jona
  • 1,218
  • 1
  • 10
  • 20
  • Read [5.6.2. String Formatting Operations — Python 2.7.17 documentation](https://docs.python.org/2/library/stdtypes.html#string-formatting) – user202729 Jan 29 '20 at 09:12
  • you should read [this](https://docs.python.org/2/library/stdtypes.html#string-formatting-operations) – Clément Jan 29 '20 at 09:12
  • Thanks for taking the time sending these links.! I'll check them out. – LukeRhinehart_ Jan 29 '20 at 09:20
  • You may be interested to review https://pyformat.info/ Also note that as of python 3.6 latest and most convenient method is to use f-strings. – buran Jan 29 '20 at 09:32

2 Answers2

0

Here %d inside the string is a format specifier which means that it will be replaced with the integer value provided after the end of the string. It is just a placeholder for the year value.

Lokesh P
  • 91
  • 1
  • 5
-1

%d means you are expecting an integer int32 after the string which in your case is the year after each print statement.

for the same example you cna use Format method as well

print ("is a Leap Year : {}".format(year))
programming freak
  • 859
  • 5
  • 14
  • 34