반응형
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)!
언급URL : https://stackoverflow.com/questions/826948/syntax-error-on-print-with-python-3
반응형
'programing' 카테고리의 다른 글
상태 배열에서 항목을 삭제하는 방법 (0) | 2022.09.23 |
---|---|
MySQL 테이블의 열 크기를 수정하려면 어떻게 해야 합니까? (0) | 2022.09.23 |
Concurrent Skip List Map은 언제 사용해야 합니까? (0) | 2022.09.23 |
JavaScript - 현재 날짜에서 주의 첫 번째 요일을 가져옵니다. (0) | 2022.09.23 |
Ubuntu에 mysql gem을 설치하는 데 문제가 있습니다. (0) | 2022.09.23 |