Python 콘솔에서 다음을 입력합니다.
>>> "\n".join(['I', 'would', 'expect', 'multiple', 'lines'])
제공 :
'I\nwould\nexpect\nmultiple\nlines'
이러한 결과를 기대하지만 :
I
would
expect
multiple
lines
내가 여기서 무엇을 놓치고 있습니까?
답변:
print결과를 잊었습니다 . 당신이 얻을 것은 인 P에RE(P)L 실제 인쇄 결과 입니다.
Py2.x에서는 다음과 같이해야합니다.
>>> print "\n".join(['I', 'would', 'expect', 'multiple', 'lines'])
I
would
expect
multiple
lines
그리고 Py3.X에서 print는 함수이므로
print("\n".join(['I', 'would', 'expect', 'multiple', 'lines']))
이제 그것은 짧은 대답이었습니다. 실제로 REPL 인 Python 인터프리터는 항상 실제 표시되는 출력이 아닌 문자열 표현을 표시합니다. 표현은 repr진술로 얻을 수있는 것입니다 .
>>> print repr("\n".join(['I', 'would', 'expect', 'multiple', 'lines']))
'I\nwould\nexpect\nmultiple\nlines'
당신이해야 할 print그 출력을 얻을 수 있습니다.
당신은해야합니다
>>> x = "\n".join(['I', 'would', 'expect', 'multiple', 'lines'])
>>> x # this is the value, returned by the join() function
'I\nwould\nexpect\nmultiple\nlines'
>>> print x # this prints your string (the type of output you want)
I
would
expect
multiple
lines