TIL[26]. Python_ Concatenating Text Strings

jake.log·2020년 8월 22일
0

Python

목록 보기
8/39

08.Concatenating Text Strings

숫자와 마찬가지로 string 도 더할 수 있다.
이를 string concatenation 이라고 한다.

2개 혹은 그 이상의 문자열들을 잇는걸 뜻합니다.

예시)

print("Hello, " + "World")

string concatenation 은 언제 사용하면 좋을까?
특정 문자열만 변수에 저장되어 있을 때 사용하면 편리하다.

예시)

name = input()

print("Hello, " + name)

만일 "jake" 란 값이 input으로 입력되면 "Hello, jake" 가 출력이 된다.

복잡한 string concatenation

literal string interpolation

name = input() 

print(f"Hello, {name}")

문법 :

  • 먼저 따옴표 앞에 "f" 를 붙인다.
  • f를 붙이면 파이썬은 f 다음에 오는 string 값을 literal string interpolation 이라고 인지하고, string 안에 있는 변수들을 실제 값으로 치환한다.
  • 치환 하고 싶은 변수 (혹은 변수가 아니어도 된다. 예를 들어 함수 호출이 될 수도 있다) 를 중괄호를 사용해서 표시한다.

예시)

date            = 1980
python_inventor = "Guido van Rossum"
location        = "Centrum Wiskunde & Informatica"
country         = "Netherlands"

print(f"""Python was conceived in the late {date}s 
by {python_inventor} at {location} (CWI) in the {country} as a successor 
to the ABC language (itself inspired by SETL), capable of exception handling 
and interfacing with the Amoeba operating system. 
Its implementation began in December 1989.""")
profile
꾸준히!

0개의 댓글