Unpythonic way of printing variables in Python?

前端 未结 5 2004
时光取名叫无心
时光取名叫无心 2021-01-02 16:51

Someone has recently demonstrated to me that we can print variables in Python like how Perl does.

Instead of:

print(\"%s, %s, %s\" % (foo, bar, baz))         


        
5条回答
  •  梦毁少年i
    2021-01-02 17:47

    Since Python 3.6, you could have what you wanted.

    >>> name = "world"
    >>> print(f'hello {name}')
    hello world
    

    note the string prefix f above

提交回复
热议问题