%d? Call and Response



My code behaves correctly. I have a question about this line: print "%d squared is %d." % (n, squared)

But I don't understand why it says "%d squared is %d" instead of "% squared is %." What do the lowercase d's add, given that % is defined without the d immediately afterward?

I have the feeling I'm overlooking or forgetting something simple and appreciate your undulgence.

Thanks, Ed

Replace this line with your code.


The letter tells Python what data type to expect. d stands for digits. The expected values are integers.


Thank you for the response!

Why would that be necessary? The program squares and prints a number, and there seems to be no reason to involve digits in it. Besides, up to now, it hasn't been necessary to declare a data type.

Are there special circumstances inside a function?


A number consists of digits (not to be confused with our fingers). In Python, %d is the placeholder for an integer. %f is a placeholder for a float. %s is a string, %r is a representation.

This page on string formatting is a good read. It is also a good idea to look up the documentation on Python's site, as well.

No different than anywhere else. Data types are a big part of programming that will become more clear as you progress.


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.