python3语法错误python_[大数据]Python 3.x中使用print函数出现语法错误(SyntaxError: invalid syntax)的原因 - 码姐姐找文...
在安裝了最新版本的Python 3.x版本之后,
去參考別人的代碼(基于Python 2.x寫的教程),去利用print函數(shù),打印輸出內(nèi)容時,結(jié)果卻遇到print函數(shù)的語法錯誤:
SyntaxError: invalid syntax
這是因為Python 2.x升級到Python 3.x,print函數(shù)的語法變化了,所以用Python 2.x的print函數(shù)的代碼,放在Python 3.x中運行,結(jié)果就出現(xiàn)了print函數(shù)的“SyntaxError: invalid syntax”了。
Python 2.x和Python 3.x中print函數(shù)語法方面的區(qū)別
最簡潔的解釋為:
Python 2.x: print “所要打印的內(nèi)容” , 不帶括號
Python 3.x: print函數(shù)(”所要打印的內(nèi)容”),必須帶括號
舉例來說明,即為:
1.不帶百分號格式化的
python 2.x:
print "Pyhon 2 can use print string without ()";
python 3.x:
print("Python3, print must use () to output string");
2.帶百分號格式化的
Python 2.x:
print "old %s version is %d, print no ()"%("Python", 2);
Python 3.x:
print("new %s version is %d, print must have ()"%("Python", 3));
總結(jié)
以上是生活随笔為你收集整理的python3语法错误python_[大数据]Python 3.x中使用print函数出现语法错误(SyntaxError: invalid syntax)的原因 - 码姐姐找文...的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: keil 函数 默认 外部 内部 博客_
- 下一篇: python商品评论分析_亚马逊产品情感