What did I miss with the usage of "%" when printing data in python

I missed somthing… I don’t know how and why to use the “%”, I did the Linux for begginers and now I am on Python for Robotics and I have seen usage of “%d” and “%f”. Help me to find out what I missed please <3

Hi @daniel.cn1993,

Welcome to our community.

%d, %f, and %s are used to print integer, float, and string variables respectively.

If you write:

print("The value is X and I love it..")

Python (and other programming languages) need to know how to override X with something. If you just write “s”, python would print only the letter “s” instead of the value of the variable that you want to print.

By putting %s, python will know that in that place it has to be put the value of a variable.

That is why you have:

variable = 'The Construct'
print("I love %s." % variable)

which results in: I love The Construct.

instead of:

variable = 'The Construct'
print("I love s." % variable)

which would result in:

I love s.

Thank you verry much !
I still did not understand why and when to use %d and %f ?
Sorry for newbe…

Hi @daniel.cn1993,

yes, it’s not really clear, but “d” is for “digit”, “f” for “float”, etc.

You can use what I extracted from here and pasted below:

Here are some commonly used conversion specifiers (not a comprehensive list):

  %d    int (signed decimal integer) 
  %u    unsigned decimal integer 
  %f    floating point values (fixed notation) - float, double 
  %e    floating point values (exponential notation) 
  %s    string 
  %c    character   

For python language, you can always use “%s” if you are not sure because printing any value as “string” will always work. If you try to print a string value using “%f” for float, you would have an error. But printing a float number with “%s” will always work.