문제

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

>>> print "hello World"
  File "<stdin>", line 1
    print "hello World"
                      ^
SyntaxError: invalid syntax
도움이 되었습니까?

해결책

Python 3에서 print 기능이되었습니다. 이것은 아래에 언급 된 것처럼 지금 괄호를 포함해야한다는 것을 의미합니다.

print("Hello World")

다른 팁

Python 3.0을 사용하는 것 같습니다. 인쇄는 호출 가능한 기능으로 바뀌 었습니다 진술보다는.

print('Hello world!')

Python 3에서 print statement a로 대체되었습니다 print() function, 기존 인쇄 문의 특수 구문의 대부분을 대체하기위한 키워드 인수와 함께. 그래서 당신은 그것을 다음과 같이 써야합니다

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의 새로운 기능은 무엇입니까?

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top