본문 바로가기
C.W.K.
Stream
Lesson 03 of 08 · published

String — 네 형태, 한 규칙 (항상 quote)

~10 min · toml, strings

Level 0평문
0 XP0/64 lessons0/12 achievements
0/100 XP to next level100 XP to go0% complete

TOML string 은 항상 quote 입어

Basic string — 큰따옴표 + escape

"hello\nworld" 처럼 써. escape 는 JSON 이랑 똑같아 (\n \t \\ \" \uXXXX). 짧은 문자열이면 이걸 기본으로 잡아.

멀티라인 basic string — 큰따옴표 세 개

""" line one\nline two """ 형태야. escape 가 그대로 먹고, 줄 끝에 \ 를 붙이면 뒤따라오는 공백을 걷어내. 진짜 줄바꿈을 넣지 않으면서 긴 줄만 접고 싶을 때 요긴해.

Literal string — 작은따옴표, escape 없음

'C:\Users\Pippa' 는 적힌 글자 그대로야. 백슬래시를 해석하지도 않고 특별한 뜻을 붙이지도 않아. Windows 경로나 regex 패턴에 딱 맞아.

멀티라인 literal string — 작은따옴표 세 개

'''line one\nline two''' 로 써. 안에 뭘 넣든 파서가 바이트 단위로 그대로 지켜줘. 여는 fence 바로 뒤의 줄바꿈 하나만 걷어내고.

원칙: 경로나 regex, Windows 식 백슬래시가 든 값에는 작은따옴표 literal string 을 써. escape 를 아예 안 한다는 규칙 하나로 오타 한 부류가 통째로 사라져. \n 이나 Unicode escape 가 필요하면 큰따옴표 basic string 을 쓰고. 내용 자체가 여러 줄이면 그때 멀티라인 형태로 가.

Code

네 string 형태 모두·toml
basic     = "with \"escapes\" and \u00e9 and \n newline"
literal   = 'C:\Users\Pippa\config.txt'

block_basic = """
This is
multi-line basic. \u00e9 works,
and backslash-newline (\) at end of a line\
folds away.
"""

block_literal = '''
This is multi-line literal.
No escapes. The exact bytes you see
are what the parser delivers.
'''
Windows 경로 — literal 이김·toml
# 고통: 모든 백슬래시 두 배
basic_path = "C:\\Users\\Pippa\\.config\\app.toml"

# 깨끗: literal string, escape 없음
literal_path = 'C:\Users\Pippa\.config\app.toml'
멀티라인 basic 의 줄 연결·toml
# \ 없으면 string 이 literal 줄바꿈 포함:
verbose = """
Line one and line two with a real newline between them.
"""

# 줄 끝의 \ 로 줄바꿈 + leading 공백 trim
folded = """
Line one and line two \
  joined as one logical line.
"""
# folded == "Line one and line two joined as one logical line.\n"

External links

Exercise

config 값 네 개를 써봐. Windows 경로 하나, 백슬래시가 든 regex 패턴 하나, 여러 줄짜리 shell script 하나, 그리고 Unicode emoji escape 가 들어간 한 줄 인사 하나. 각각에 맞는 string 형태를 골라서 쓰고, tomllib 이나 tomli 로 파싱해서 값이 의도한 대로 나오는지 하나씩 확인해.

Progress

Progress is local-only — sign in to sync across devices.
이 페이지에서 버그를 발견하셨거나 피드백이 있으세요?문제 신고

댓글 0

🔔 답글 알림 (로그인 필요)
로그인댓글을 남기려면 로그인해 주세요.

아직 댓글이 없어요. 첫 댓글을 남겨보세요.