🎯 1. 문자열 기본

문자열 생성

# 방법 1: 작은따옴표/큰따옴표
s = 'hello'
s = "hello"

# 방법 2: 여러 줄
s = '''hello
world'''

s = """hello
world"""

# 방법 3: 문자열 변환
s = str(123)            # '123'
s = str([1, 2, 3])      # '[1, 2, 3]'

# 빈 문자열
s = ''
s = ""

문자열은 불변(Immutable)!

s = "hello"
s[0] = 'H'              # TypeError!  ❌ 변경 불가!

# ✅ 새로운 문자열 생성
s = 'H' + s[1:]         # 'Hello'

🎯 2. split() - 문자열 나누기 ⭐⭐⭐

기본 사용법

s = "hello world python"

# 공백 기준 분리 (기본)
words = s.split()       # ['hello', 'world', 'python']

# 특정 문자 기준 분리
s = "apple,banana,cherry"
fruits = s.split(',')   # ['apple', 'banana', 'cherry']

s = "2024-01-15"
date = s.split('-')     # ['2024', '01', '15']

# 분리 횟수 제한
s = "a:b:c:d:e"
parts = s.split(':', 2) # ['a', 'b', 'c:d:e']  # 최대 2번만 분리

입력 처리에 필수! ⭐⭐⭐

# 코테 입력 받기 패턴
n = int(input())                    # 정수 1개
arr = list(map(int, input().split()))  # 정수 여러 개

# 문자열 리스트
words = input().split()             # 공백으로 분리

# 특정 개수만 받기
a, b = map(int, input().split())    # 2개
a, b, c = map(int, input().split()) # 3개

# 여러 줄 입력
n = int(input())
data = [input().split() for _ in range(n)]

split() vs splitlines()

s = "hello\\nworld\\npython"

# split('\\n')
s.split('\\n')           # ['hello', 'world', 'python']

# splitlines()
s.splitlines()          # ['hello', 'world', 'python']  # 더 안전

# 차이점
s = "hello\\nworld\\n"
s.split('\\n')           # ['hello', 'world', '']  # 빈 문자열 포함
s.splitlines()          # ['hello', 'world']      # 빈 문자열 제외

🎯 3. join() - 문자열 합치기 ⭐⭐⭐

기본 사용법

words = ['hello', 'world', 'python']

# 공백으로 합치기
s = ' '.join(words)     # 'hello world python'

# 콤마로 합치기
s = ', '.join(words)    # 'hello, world, python'

# 빈 문자열로 합치기
s = ''.join(words)      # 'helloworldpython'

# 특수 문자로 합치기
s = '-'.join(words)     # 'hello-world-python'
s = '\\n'.join(words)    # 'hello\\nworld\\npython'

리스트를 문자열로 (코테 자주 씀!)

# 숫자 리스트 → 문자열
arr = [1, 2, 3, 4, 5]
s = ''.join(map(str, arr))      # '12345'
s = ' '.join(map(str, arr))     # '1 2 3 4 5'

# 출력에 유용
print(' '.join(map(str, arr)))  # 1 2 3 4 5
print(*arr)                      # 1 2 3 4 5  # 더 간단!