🎯 1. 입력 (input) ⭐⭐⭐
기본 입력
# 문자열 입력 (자동으로 개행 제거)
s = input() # "hello" 입력 시 → 'hello'
# 정수 입력
n = int(input()) # "10" 입력 시 → 10
# 실수 입력
f = float(input()) # "3.14" 입력 시 → 3.14
# 여러 개 입력 (공백 구분)
a, b = map(int, input().split())
# "3 5" 입력 시 → a=3, b=5
a, b, c = map(int, input().split())
# "1 2 3" 입력 시 → a=1, b=2, c=3
리스트 입력 ⭐⭐⭐
# 정수 리스트
arr = list(map(int, input().split()))
# "1 2 3 4 5" → [1, 2, 3, 4, 5]
# 문자열 리스트
words = input().split()
# "apple banana cherry" → ['apple', 'banana', 'cherry']
# 한 글자씩 리스트로
s = list(input())
# "hello" → ['h', 'e', 'l', 'l', 'o']
# 숫자를 한 글자씩
digits = list(map(int, input()))
# "12345" → [1, 2, 3, 4, 5]
여러 줄 입력 ⭐⭐⭐
# n줄 입력
n = int(input())
data = [input() for _ in range(n)]
# 정수 n줄
n = int(input())
numbers = [int(input()) for _ in range(n)]
# 2차원 배열 입력
n = int(input())
matrix = [list(map(int, input().split())) for _ in range(n)]
# 입력:
# 3
# 1 2 3
# 4 5 6
# 7 8 9
# → [[1,2,3], [4,5,6], [7,8,9]]
# 문자열 2차원 배열
n = int(input())
grid = [list(input()) for _ in range(n)]
# 입력:
# 3
# abc
# def
# ghi
# → [['a','b','c'], ['d','e','f'], ['g','h','i']]
EOF까지 입력 받기
# 방법 1: try-except
import sys
data = []
try:
while True:
data.append(input())
except EOFError:
pass
# 방법 2: sys.stdin (더 빠름!)
import sys
data = sys.stdin.read().splitlines()
# 방법 3: 한 줄씩
import sys
for line in sys.stdin:
line = line.strip()
# 처리...
빠른 입력 ⭐⭐⭐
# 많은 입력이 있을 때 필수!
import sys
input = sys.stdin.readline
# 이제 input() 사용하면 빠름
n = int(input())
arr = list(map(int, input().split()))
# 주의: readline()은 개행 포함하므로 strip() 필요할 수도
s = input().strip()
🎯 2. 출력 (print) ⭐⭐⭐
기본 출력
# 기본
print("Hello World") # Hello World
# 여러 개 출력 (공백으로 구분)
print(1, 2, 3) # 1 2 3
print("a", "b", "c") # a b c
# 변수 출력
a, b = 10, 20
print(a, b) # 10 20
구분자 변경 (sep)
# 기본 구분자: 공백
print(1, 2, 3) # 1 2 3
# 구분자 변경
print(1, 2, 3, sep=',') # 1,2,3
print(1, 2, 3, sep='-') # 1-2-3
print(1, 2, 3, sep='\\n') # 1
# 2
# 3
# 빈 구분자
print(1, 2, 3, sep='') # 123
줄바꿈 변경 (end)
# 기본: 줄바꿈 (\\n)
print("Hello")
print("World")
# Hello
# World
# end 변경
print("Hello", end=' ')
print("World")
# Hello World
print("Hello", end='')
print("World")
# HelloWorld
# 여러 줄을 한 줄로
for i in range(5):
print(i, end=' ') # 0 1 2 3 4
print() # 마지막에 줄바꿈
리스트 출력 ⭐⭐⭐