Python Forum
Operator meaning explanation - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Operator meaning explanation (/thread-34438.html)



Operator meaning explanation - Sherine - Jul-31-2021

Hi,

I have a question.

name = "Giraffe"
score = 80
print("%s result as %d" % (name, score))
Output:
Giraffe result as 80
What meaning for the % sign place before (name, score)?

Thanks


RE: Operator meaning explanation - ndc85430 - Jul-31-2021

It's just the old way of allowing you to substitute the values of those variables for the placeholders (%s and %d) in the string, as the output shows. These days, you should be using f-strings, though.


RE: Operator meaning explanation - Larz60+ - Jul-31-2021

print("%s result as %d" % (name, score))
is old style print statement, you should use f-string:
print(f"{name} result as {score}")


RE: Operator meaning explanation - Sherine - Jul-31-2021

(Jul-31-2021, 07:52 AM)ndc85430 Wrote: It's just the old way of allowing you to substitute the values of those variables for the placeholders (%s and %d) in the string, as the output shows. These days, you should be using f-strings, though.

Thank you so much , I learn new thing outside the book. Wink
(Jul-31-2021, 08:34 AM)Larz60+ Wrote: print("%s result as %d" % (name, score))
is old style print statement, you should use f-string:
print(f"{name} result as {score}")

Thank you so much, learn new thing. Dance it means my book is old version.. Big Grin Big Grin Big Grin