【问题标题】:How to use this code in python 3 version? [duplicate]如何在 python 3 版本中使用此代码? [复制]
【发布时间】:2014-06-17 18:27:07
【问题描述】:
我想在 python 3 中运行此代码,但我不能。每当我尝试运行代码时,我都会收到无效的语法错误。
age = 20
name = 'Swaroop'
print '{} was {} years old when he wrote this book'.format(name, age)
print 'Why is {} playing with that python?'.format(name)
请帮帮我。
谢谢。
【问题讨论】:
标签:
python
methods
syntax
format
【解决方案1】:
在print 函数调用周围加上括号。
print('{} was {} years old when he wrote this book'.format(name, age))
print('Why is {} playing with that python?'.format(name))
在 Python2 中,print 是一个语句,不需要括号。
In Python3, print is a function,因此它的参数需要括号。