2

通常、次のコードを使用して、文字列内の変数を実装できます

print "this is a test %s" % (test)

ただし、これを使用する必要があったため、機能していないようです

from __future__ import print_function
4

3 に答える 3

4
>>> test = '!'
>>> print "this is a test %s" % (test)
this is a test !

If you import print_function feature, print acts as function:

>>> from __future__ import print_function
>>> print "this is a test %s" % (test)
  File "<stdin>", line 1
    print "this is a test %s" % (test)
                            ^
SyntaxError: invalid syntax

You should use function call form after the import.

>>> print("this is a test %s" % (test))
this is a test !

SIDE NOTE

According to the documentation:

str.format is new standard in Python 3, and should be preferred to the % formatting.

>>> print("this is a test {}".format(test))
this is a test !
于 2013-09-27T14:43:30.940 に答える
3

これを試して:

print("this is a test", test)

またはこれ:

print("this is a test {}".format(test))
于 2013-09-27T14:42:19.633 に答える
0

文字列を実装している場合は、%s.

于 2013-09-27T14:41:02.353 に答える