How to print as printf in python3?

In python 2 I used

print "a=%d,b=%d" % (f(x,n),g(x,n)) 

Ive tried

 print("a=%d,b=%d") % (f(x,n),g(x,n)) 
+62
python string
Oct 18 '13 at 19:03
source share
9 answers

In Python2, print was a keyword that stated a statement:

 print "Hi" 

In Python3, print is a function that can be called:

 print ("Hi") 

In both versions, % is an operator that requires a string on the left side and a value or tuple of values ​​or a matching object (for example, dict ) on the right side.

So your line should look like this:

 print("a=%d,b=%d" % (f(x,n),g(x,n))) 

In addition, the recommendation for Python3 and new is to use {} -style formatting instead of % -line formatting:

 print('a={:d}, b={:d}'.format(f(x,n),g(x,n))) 
+102
Oct 18 '13 at 19:04 on
source share

The most recommended way is to use the format method. Read more about it here.

 a, b = 1, 2 print("a={0},b={1}".format(a, b)) 
+33
Oct 18 '13 at 19:06 on
source share

The simple printf () function from O'Reilly Python Cookbook .

 import sys def printf(format, *args): sys.stdout.write(format % args) 

Output Example:

 i = 7 pi = 3.14159265359 printf("hi there, i=%d, pi=%.2f\n", i, pi) # hi there, i=7, pi=3.14 
+11
Jun 16 '16 at 1:19
source share

A simple example:

print("foo %d, bar %d" % (1,2))

+6
Apr 22 '14 at 17:15
source share

Python 3.6 introduced f-lines for inline interpolation. What's even nicer, he expanded the syntax to also allow interpolation format specifiers. Something I was working on while I was looking for this (and stumbled upon this old question!):

 print(f'{account:40s} ({ratio:3.2f}) -> AUD {splitAmount}') 

PEP 498 contains data. And ... he sorted my favorite peeve with format specifiers in other languages ​​- allows specifications that can themselves be expressions! Hurrah! See Format Specifiers .

+6
Sep 02 '17 at 11:54 on
source share

More simple.

 def printf(format, *values): print(format % values ) 

Then:

 printf("Hello, this is my name %s and my age %d", "Martin", 20) 
+3
Jan 20 '17 at 13:29
source share

Since your % is outside the print(...) parentheses, you are trying to insert variables into the result of your print call. print(...) returns None , so it won’t work, and a small number of you, having already printed your template, are forbidden by the laws of the universe in which we live by this time and time travel.

Everything you want to print, including % and its operand, must be inside your print(...) call so that the line can be built before it is printed. A.

 print( "a=%d,b=%d" % (f(x,n), g(x,n)) ) 

I added a few extra spaces to make them clearer (although they are not needed and are usually not considered good style).

+1
Oct 18 '13 at 19:13
source share

Other printf words are missing in python ... I'm surprised! Best code

 def printf(format, *args): sys.stdout.write(format % args) 

Because of this form, you may not want to print \ n. All the rest are not. This is why printing is a poor operator. And also you need to write args in a special form. There are no flaws in the function above. This is the standard regular form of the printf function.

0
Nov 22 '17 at 21:14
source share
 print("Name={}, balance={}".format(var-name, var-balance)) 
-one
Oct 24 '16 at 2:49 on
source share



All Articles