How to print like printf in Python3?

前端 未结 9 1981
粉色の甜心
粉色の甜心 2020-11-29 16:52

In Python 2 I used:

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

I\'ve tried:

print(\"a=%d,b=%d\") % (f(x,n),g(x,n))
相关标签:
9条回答
  • 2020-11-29 17:22

    Simple Example:

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

    0 讨论(0)
  • 2020-11-29 17:24

    Other words printf absent in python... I'm surprised! Best code is

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

    Because of this form allows not to print \n. All others no. That's why print is bad operator. And also you need write args in special form. There is no disadvantages in function above. It's a standard usual form of printf function.

    0 讨论(0)
  • 2020-11-29 17:28

    A simpler one.

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

    Then:

    printf("Hello, this is my name %s and my age %d", "Martin", 20)
    
    0 讨论(0)
提交回复
热议问题