使用 print 语句需要一些帮助
Need some help using print statement
我试过这段代码:
x = 5
y = "Anshul"
print(x)
print(' ',y)
它给出了输出:
5
(' ', 'Anshul')
但为什么输出不是这样的:
5
Anshul
我在我的名字前添加了''
以获得space,但它没有用。
有什么办法可以给space?
尝试使用 format()
:
print(' {}'.format(y))
尝试这样的事情:
print(' ' + y)
最好的办法是使用字符串插值。
参见:String Interpolation
您的代码应如下所示:
print(' %s' % y) # option 1
print(' {}'.format(y)) # option 2
我试过这段代码:
x = 5
y = "Anshul"
print(x)
print(' ',y)
它给出了输出:
5
(' ', 'Anshul')
但为什么输出不是这样的:
5
Anshul
我在我的名字前添加了''
以获得space,但它没有用。
有什么办法可以给space?
尝试使用 format()
:
print(' {}'.format(y))
尝试这样的事情:
print(' ' + y)
最好的办法是使用字符串插值。 参见:String Interpolation
您的代码应如下所示:
print(' %s' % y) # option 1
print(' {}'.format(y)) # option 2