programing

Python 3에서 인쇄 시 구문 오류 발생

yoursource 2022. 9. 23. 22:46
반응형

Python 3에서 인쇄 시 구문 오류 발생

Python 3에서 문자열을 인쇄할 때 구문 오류가 발생하는 이유는 무엇입니까?

>>> print "hello World"
  File "<stdin>", line 1
    print "hello World"
                      ^
SyntaxError: invalid syntax

Python 3에서는print 기능이 되었습니다.즉, 다음과 같이 괄호를 포함해야 합니다.

print("Hello World")

Python 3.0을 사용하고 있는 것 같습니다.Python 3.0에서는 인쇄가 스테이트먼트가 아닌 호출 가능한 함수로 바뀌었습니다.

print('Hello world!')

파이썬 3에서는print statement로 대체되었습니다.print() function오래된 print 스테이트먼트의 특수 구문을 대부분 대체하는 키워드 인수를 지정합니다.그래서 이렇게 써야 돼요.

print("Hello World")

그러나 만약 당신이 이것을 프로그램에 썼을 때 Python 2.x를 사용하는 누군가가 그것을 실행하려고 하면, 그들은 오류를 얻을 것이다.이를 방지하려면 인쇄 기능을 가져오는 것이 좋습니다.

from __future__ import print_function

코드가 2.x와 3.x 모두에서 동작하게 되었습니다.

print() 함수에 익숙해지려면 다음 예도 참조하십시오.

Old: print "The answer is", 2*2
New: print("The answer is", 2*2)

Old: print x,           # Trailing comma suppresses newline
New: print(x, end=" ")  # Appends a space instead of a newline

Old: print              # Prints a newline
New: print()            # You must call the function!

Old: print >>sys.stderr, "fatal error"
New: print("fatal error", file=sys.stderr)

Old: print (x, y)       # prints repr((x, y))
New: print((x, y))      # Not the same as print(x, y)!

출처: Python 3.0의 신기능

언급URL : https://stackoverflow.com/questions/826948/syntax-error-on-print-with-python-3

반응형