본문 바로가기
목차
Python

Python String Literal Concatenation

by ds31x 2026. 5. 23.
728x90
반응형

comma 가 문자열 literal 사이에 없음을 주의

 

Python은 서로 붙어 있는 문자열 리터럴(string literal)을 자동으로 하나의 문자열로 합침.

print("test" "dddd")

 

위 코드는 다음처럼 처리됨.

print("testdddd")

 

이를 string literal concatenation이라고 함.

 

이 결합은 runtime에 "test" + "dddd"를 수행하는 것이 아니라,

Python source code가 bytecode로 바뀌는 compile time에 처리됨.


핵심 조건

  • 문자열(str) 리터럴끼리만 가능
  • variable과는 불가능
  • + 없이 인접한 문자열이 자동 결합됨
  • compile time에 하나의 문자열로 처리됨

긴 문자열을 여러 줄로 나눌 때 자주 사용함.

message = (
    "This is a long message. "
    "It is split across multiple lines."
)

Variable과는 자동 결합 안 됨

variable는 자동 결합되지 않음.

a = "test"

print(a "dddd")  # SyntaxError

variable과 결합시키려면 +나 f-string을 사용함.

print(a + "dddd")
print(f"{a}dddd")

list 나 tuple 내에선 주의

items = [
    "apple",
    "banana"
    "cherry",
]

위 코드는 에러가 나지 않고 다음처럼 처리됨.

items = [
    "apple",
    "bananacherry",
]

"banana" 뒤에 comma가 빠졌기 때문임.


요약

  • "a" "b""ab"와 같음.
  • string literal concatenation은 compile time에 이루어짐.
  • variable에는 적용되지 않음.
  • 긴 고정 문자열을 여러 줄로 나눌 때 유용함.
  • list, tuple 안에서는 comma 누락에 주의해야 함.
728x90

'Python' 카테고리의 다른 글

Lock and GIL  (0) 2026.06.01
Dynamic Scope 란?  (0) 2026.05.24
tqdm 간단 사용법  (0) 2026.05.23
uv 를 통한 wheel 빌드하기-uv_build, hatchling  (0) 2026.05.04
pip install 옵션 정리  (0) 2026.05.03