코딩테스트를 위한 출력방법

iamjinseo·2022년 8월 9일
0

문제풀이-Python

목록 보기
2/134
post-thumbnail

리스트 대괄호 없이 출력

print(*list)
파이썬의 언패킹을 이용한 코드이다. 작동 원리 참고
예를들어 list == [1,2,3,4,5]이면 print(*list) == print(1,2,3,4,5)
=>따라서 각 원소가 공백으로 나뉘어 출력된다.

리스트 공백없이 출력

print(*list, sep='')
print(''.join(list))

리스트 줄바꿈하여 출력

print(*list, sep='\n')

if문으로 한 줄 출력

if ~~ : 
	print('a')
else :
	print('b')

=>

print( 'a' if ~~ else 'b' ) 

빠르게 출력

import sys
print = sys.stdout.write

print("%s\n" % "123")  # 123
print("%s\n" % ("12" + "3"))  # 123
print("%d + %d = %d\n" % (1, 2, 1 + 2))  # 1 + 2 = 3

참고 - https://urakasumi.tistory.com/m/273

profile
일단 뭐라도 해보는 중

0개의 댓글