Python print(end = “” ) error가 일어날 경우
python2.7에서 공부했던 것들을 복습하기 위해 print로 이것 저것 시험해보던 중에 다음과 같은 Syntax Error가 발생했다.
vi hi.py # coding: utf-8 print("hello, world") print("hello, ", end="") print("world") |
[root@localhost ~]# python hi.py File "hi.py", line 4 print("hello, ", end="") ^ SyntaxError: invalid syntax |
우리의 구글님에게 물어보니, 친절하게도 다음과 같은 구문을 추가하면 해결 가능하다는 것!
from __future__ import print_function |
이 future모듈을 사용하는 것으로 python3의 기능처럼 코딩을 할 수 있다고 한다.
특히 python3이 가지고 있지만, python2에는 없는 기능을 호환하면서 사용할 수 있기 때문에
python2를 지우기는 뭣하고 python3처럼 사용하고 싶을 때 쓸 수 있다는..
어쨌던 아래와 같이 구문을 추가해 보았다.
# coding: utf-8 from __future__ import print_function print("hello, world") print("hello, ", end="") print("world") |
그랬더니!! 되었당 ><
[root@localhost ~]# python hi.py hello, world hello, world |
알고보니, python2에서는 행 바꾸기를 하는 문자를 출력하지 않는 경우에는 print "hoge"와 같이 기술했으나,
python3에서는 end=''의 ''부분을 임의의 문자열로 바꿔서 사용한다고 한다.
즉, 나와 같이 python3에서 사용하는 구문을 그대로 사용하고 싶은 경우에는
위와 같이 future모듈을 이용한 구문을 추가해서 사용하면 된다.
이 지점에서.. 하.. 다시 python3을 추가로 설치해서 공부해야 하나하는.. 막역한.. 기분이...
참고 사이트 (영어,일본어)
Python print(end = "" ) error
I am writing this code while learning from online videos. The issue as after running the code I am getting errors with the last else indentation and the print("string",end = ""). I just can't figur...
stackoverflow.com
__future__ モジュールについて - さりんじゃーのプログラミング日記
1 2 3 4 5 6 >>> zip([1, 2, 3, 4, 5],[5, 4, 3, 2, 1]) zip([1, 2, 3, 4, 5],[5, 4, 3, 2, 1]) >>> list(zip([1, 2, 3, 4, 5],[5, 4, 3, 2, 1])) list(zip([1, 2, 3, 4, 5],[5, 4, 3, 2, 1])) [(1, 5), (2, 4), (3, 3), (4, 2), (5, 1)]
salinger.github.io
python 2.7でPython 3風に書きたい - 唯物是真 @Scaled_Wurm
Python 3以降との違いを調べていたときのメモ Python 2.7でも試せるもの 以下にPython 2.7でimportをすればPython 3風に書けるものを列挙しておきます from __future__ import division Python 2.7では割り算「/」の結果は切り捨てでしたが、Python 3以降ではちゃんと小数点以下まで保持されるようになります 以前のような切り捨ての割り算は「//」演算子でできるようになります >>> 3 / 2 1 >>> from __future_
sucrose.hatenablog.com