What does the percentage mark mean


#1

Hey all, I just wonder shortly what the percentage (%) i wrote in bold text does.

Why does it need to be one after all the others outside the apostrophe

edit: i could not write it in bold text but it is withing 2 stars

print '%s/%s/%s %s:%s:%s' **%** (now.month, now.day, now.year, now.hour, now.minute, now.second)

#2

@mrfatdog21: Found an interesting explanation about it, here's part of it.

Link: The string format operator

Hope it helps you! :slight_smile:

Note: the order you give on that sequence of elements within the parentheses will tell your program at which position a given value should be replaced. Try to switch positions within them and see the result.


#3

@mrfatdog21

That is not the sole way to format string, you have that form and the following,

Python Code:

a_string = 'String Me!'
a_number = 51
a_float = 3.141592653589793

print("{}: {}: {:.2f}".format(a_string, a_number, a_float))
# OUTPUT: String Me!: 51: 3.14

There are some more advanced features you can do, just google string formatting and you will find tons of info on it. (Docs)

Best of luck!

As a side note, you can also use string formatting and some special methods of print to center text and all types of things.

Python Center:

print("{}: {}: {:.2f}".format(a_string, a_number, a_float).center(50))
# OUTPUT:                String Me!: 51: 3.14

#4

@zeziba: I believe that, according to what mrfatdog21 asked, the explanation about % in string formatting should be enough to answer his questions.
Of course there are more ways to do it but I believe it would be more simple to simply show that one but thanks for the reminder.