1 ๋ถ„ ์†Œ์š”

r-string?

์–ด๋–ค ๊ฒฝ์šฐ์—๋Š” ๋ฌธ์ž์—ด์— ์ด์Šค์ผ€์ดํ”„ ์‹œํ€€์Šค(โ€\nโ€) ๊ฐ€ ํ•„์š”ํ•œ ๊ฒฝ์šฐ๊ฐ€ ์žˆ๋‹ค.

์˜ˆ๋ฅผ ๋“ค๋ฉด ํŒŒ์ผ ๊ฒฝ๋กœ ๊ฐ™์€ ๊ฒฝ์šฐ๊ฐ€ ์žˆ๋‹ค!

>>> dir1 = "D:\File"
>>> dir2 = "D:\newFile"

>>> print(dir1)
D:\File
>>> print(dir2)
D:
ewFile

dir1 ์„ print ํ•ด๋ณด๋ฉด ํŒŒ์ผ ๊ฒฝ๋กœ ๊ทธ๋Œ€๋กœ ์ถœ๋ ฅ์ด ๋˜์ง€๋งŒ, dir2 ์˜ ๊ฒฝ์šฐ์—๋Š” \n์„ ์ค„๋ฐ”๊ฟˆ ๋ฌธ์ž๋กœ ์ธ์‹ํ•ด D:์™€ ewFile๋กœ ์ค„๋ฐ”๊ฟˆ๋˜์–ด ์ถœ๋ ฅ๋œ๋‹ค.

์ด๋Ÿฐ ๊ฒฝ์šฐ, r-string์„ ์‚ฌ์šฉํ•˜๊ฑฐ๋‚˜ \๋ฅผ ์•ž์— ๋ถ™์ด๊ณ  ์ด์Šค์ผ€์ดํ”„ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•˜๋ฉด๋œ๋‹ค.

>>> new_dir1 = r"D:\newFile"
>>> new_dir2 = "D:\\newFile"

>>> print(new_dir1)
D:\newFile
>>> print(new_dir2)
D:/newFile

f-string?

f-string ์€ ๋ฌธ์ž์—ด ํฌ๋งคํŒ…(formatting) ๋ฐฉ์‹์ด๋‹ค.

๋ณ€์ˆ˜ ๊ฐ’์„ ๋ฌธ์ž์—ด ์•ˆ์— ํฌํ•จ์‹œํ‚ฌ ๋•Œ ์ž์ฃผ ์‚ฌ์šฉํ•œ๋‹ค.

>>> year = 2024
>>> new_str1 = "Hello" + " World " + year
>>> new_str2 = f"Hello World {year}"

f-string์„ ์ ์šฉํ•˜๊ธฐ ์ „ ๋ฐฉ์‹๋Œ€๋กœ ํ•˜๋ฉด new_str1 ๊ณผ ๊ฐ™์ด ์„ ์–ธํ•˜๋ฉด ๋œ๋‹ค.

ํ•˜์ง€๋งŒ ์กฐ๊ธˆ ๋” ์ง๊ด€์ ์œผ๋กœ ๋ณผ ์ˆ˜ ์žˆ๋„๋ก f-string์„ ์ ์šฉํ•ด new_str2 ์ฒ˜๋Ÿผ ์‚ฌ์šฉํ•˜๋ฉด ๋” ์ข‹๋‹ค!

: ๋’ค์— ์ •์ˆ˜๋ฅผ ์ „๋‹ฌํ•˜๋ฉด ํ•ด๋‹น ํ•„๋“œ์˜ ์ตœ์†Œ ๋ฌธ์ž ํญ์ด ๋œ๋‹ค!

์ด๊ฑด ์—ด์„ ์ค„ ๋งž์ถคํ•  ๋•Œ ํŽธํ•˜๋‹ค.

>>> table = {'Sjoerd': 4127, 'Jack': 4098, 'Dcab': 7678}
>>> for name, phone in table.items():
		print(f'{name:10} ==> {phone:10d}')

Sjoerd     ==>       4127
Jack       ==>       4098
Dcab       ==>       7678

์ถ”๊ฐ€๋กœ =๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์ด ๊ฐ’์ด ์–ด๋–ค ๊ฐ’์ธ์ง€ ์ง๊ด€์ ์œผ๋กœ ํ‘œํ˜„ํ•  ์ˆ˜ ์žˆ๋‹ค.

>>> bugs = 'roaches'
>>> count = 13
>>> area = 'living room'
>>> print(f'Debugging {bugs=} {count=} {area=}')
Debugging bugs='roaches' count=13 area='living room'

๋ฌธ์ž์—ด format()?

๋ฌธ์ž์—ด์„ ํฌ๋งคํŒ…ํ•˜๋Š” ๋ฐฉ์‹์œผ๋กœ๋Š” f-string ๋ง๊ณ ๋„ ๋˜ ์žˆ๋‹ค.

๋ฐ”๋กœ format() ๋ฉ”์„œ๋“œ๋ฅผ ์ ์šฉํ•˜๋Š” ๊ฒƒ์ด๋‹ค!

>>> print('Hello World {}!"'.format(2024))
Hello World 2024!

๋ฌธ์ž์—ด์„ ์„ ์–ธํ•˜๊ณ , ๋ณ€์ˆ˜๊ฐ€ ํ•„์š”ํ•œ ์œ„์น˜์— {}๋ฅผ ์ ๊ณ , ์ „๋‹ฌํ•  ๊ฐ’์„ .format() ์˜ ์ธ์ž๋กœ ์ „๋‹ฌํ•˜๋ฉด ๋!

๋‹ค๋ฅธ ํฌ๋งท ๋ฐฉ์‹์ด ์žˆ๋‹ค..! ์ž์ฃผ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ์‹๋งŒ ์ผ๋‹จ ์ ์—ˆ๊ณ , ์ž์„ธํ•œ ๋‚ด์šฉ์€ python ๊ณต์‹ ๋ ˆํผ๋Ÿฐ์Šค ๋ฅผ ์ฐธ๊ณ ํ•˜๋ฉด ๋œ๋‹ค.

์ฐธ๊ณ  ์ž๋ฃŒ

2.1.5 ๋ฌธ์ž์—ด

7. ์ž…๋ ฅ๊ณผ ์ถœ๋ ฅ

์นดํ…Œ๊ณ ๋ฆฌ:

์—…๋ฐ์ดํŠธ:

๋Œ“๊ธ€๋‚จ๊ธฐ๊ธฐ