동일한 라인에 새 출력 인쇄
루프된 출력물을 같은 라인으로 화면에 출력하고 싶습니다.
Python 3.x에서 가장 간단한 방법으로 어떻게 해야 합니까?
이 질문은 줄 끝에 있는 쉼표(예: print I)를 사용하여 Python 2.7에 대해 질문한 것으로 알고 있지만 Python 3.x에 대한 솔루션을 찾을 수 없습니다.
i = 0
while i <10:
i += 1
## print (i) # python 2.7 would be print i,
print (i) # python 2.7 would be 'print i,'
화면 출력.
1
2
3
4
5
6
7
8
9
10
인쇄할 내용:
12345678910
새로운 독자들도 이 링크를 방문합니다. http://docs.python.org/release/3.0.1/whatsnew/3.0.html
부터help(print):
Help on built-in function print in module builtins:
print(...)
print(value, ..., sep=' ', end='\n', file=sys.stdout)
Prints the values to a stream, or to sys.stdout by default.
Optional keyword arguments:
file: a file-like object (stream); defaults to the current sys.stdout.
sep: string inserted between values, default a space.
end: string appended after the last value, default a newline.
사용할 수 있습니다.end키워드:
>>> for i in range(1, 11):
... print(i, end='')
...
12345678910>>>
참고로 다음 작업을 수행해야 합니다.print()당신 자신의 마지막 새로운 라인.그나저나, 당신은 뒤에 쉼표가 있는 파이썬 2에서 "12345678910"을 얻지 못할 것입니다.1 2 3 4 5 6 7 8 9 10대신.
python 2.x의 경우 *
줄 바꿈을 피하려면 뒤에 있는 쉼표를 사용합니다.
print "Hey Guys!",
print "This is how we print on the same line."
위의 코드 스니펫에 대한 출력은 다음과 같습니다.
Hey Guys! This is how we print on the same line.
python 3.x의 경우 *
for i in range(10):
print(i, end="<separator>") # <separator> = \n, <space> etc.
위의 코드 스니펫에 대한 출력은 다음과 같습니다.<separator> = " "),
0 1 2 3 4 5 6 7 8 9
제안된 것과 유사하게 다음을 수행할 수 있습니다.
print(i, end=',')
출력: 0,1,2,3,
print("single",end=" ")
print("line")
이것은 출력을 제공할 것입니다.
single line
질문의 용법.
i = 0
while i <10:
i += 1
print (i,end="")
다음과 같은 작업을 수행할 수 있습니다.
>>> print(''.join(map(str,range(1,11))))
12345678910
>>> for i in range(1, 11):
... print(i, end=' ')
... if i==len(range(1, 11)): print()
...
1 2 3 4 5 6 7 8 9 10
>>>
인쇄가 다음 줄의 프롬프트 뒤에서 실행되지 않도록 하는 방법입니다.
0부터 n까지의 숫자를 동일한 줄로 인쇄하려는 예를 들어 보겠습니다.다음 코드를 사용하여 이 작업을 수행할 수 있습니다.
n=int(raw_input())
i=0
while(i<n):
print i,
i = i+1
입력 시, n = 5
Output : 0 1 2 3 4
언급URL : https://stackoverflow.com/questions/12032214/print-new-output-on-same-line
'programing' 카테고리의 다른 글
| 일, 월, 년을 정수로 지정한 SQL Server에서 날짜를 생성하는 방법 (0) | 2023.05.10 |
|---|---|
| 문자열 배열에서 모든 빈 요소 제거 (0) | 2023.05.10 |
| 게시물:제약 조건이 없는 경우 제약 조건 추가 (0) | 2023.05.10 |
| 여러 시작 프로젝트 간의 Visual Studio 지연? (0) | 2023.05.10 |
| jQuery를 사용하여 입력이 비어 있는지 확인합니다. (0) | 2023.05.10 |