• 技术文章 >常见问题 >Python常见问题

    python3 print报错

    (*-*)浩(*-*)浩2019-08-26 15:18:48原创4441

    在安装了最新版本的Python 3.x版本之后,去参考别人的代码(基于Python 2.x写的教程),去利用print函数,打印输出内容时,结果却遇到print函数的语法错误:

    SyntaxError: invalid syntax

    这是因为Python 2.x升级到Python 3.x,print函数的语法变化了,所以用Python 2.x的print函数的代码,放在Python 3.x中运行,结果就出现了print函数的“SyntaxError: invalid syntax”了。

    Python 2.x和Python 3.x中print函数语法方面的区别

    最简洁的解释为:

    Python 2.x: print “所要打印的内容” , 不带括号

    Python 3.x: print函数(”所要打印的内容”),必须带括号

    举例来说明,即为:

    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));
    专题推荐:python
    品易云
    上一篇:命令行怎么退出python 下一篇:python中的for循环语句怎么写

    相关文章推荐

    • python为什么是胶水语言

    全部评论我要评论

    © 2021 Python学习网 苏ICP备2021003149号-1

  • 取消发布评论
  • 

    Python学习网