0

So I was looking at a python tutorial, and in a statement, it used %.

print ("Total Employee %d" % Employee.empCount)

Can someone please take their time and describe to me what that means. I'm pretty sure that it doesn't signify a division.

caleb7bai
  • 1
  • 3
  • It's a C-style string formatting but it's is no longer used in python, So it's a quite old formatting technique. – Pygirl May 27 '20 at 04:59
  • Does this answer your question? [String formatting in Python](https://stackoverflow.com/questions/517355/string-formatting-in-python) – sushanth May 27 '20 at 05:02

4 Answers4

1

Python uses C-style string formatting to create new, formatted strings. The "%" operator is used to format a set of variables enclosed in a "tuple" (a fixed size list), together with a format string, which contains normal text together with "argument specifiers", special symbols like "%s" and "%d".

enter image description here

Pygirl
  • 12,969
  • 5
  • 30
  • 43
0

the % sign in this case is supposed to be used for string formatting. This is an old technique and can be used with and f-string now. % is mostly used in java and not python.

caleb7bai
  • 1
  • 3
0

Python Program for Old Style Formatting of Integers also used %

Integer1 = 12.3456789
print("Formatting in 3.2f format: ") 
print('The value of Integer1 is %3.2f' %Integer1) 
print("\nFormatting in 3.4f format: ") 
print('The value of Integer1 is %3.4f' %Integer1) 

Output:

Formatting in 3.2f format: 
The value of Integer1 is 12.35

Formatting in 3.4f format: 
The value of Integer1 is 12.3457

also use as

print("writing integer in a string: %s" %Integer1) 

output

writing integer in a string: 12.3456789
Zesty Dragon
  • 551
  • 3
  • 18
0

% Employee.empCount is a variable and %d for print integer variable.That's mean value of variable % Employee.empCount print in place of %d.

% sign use for give reference of variable.

Jay Kukadiya
  • 553
  • 6
  • 9