[Python] f-string trick (2)
ยท
๐Ÿ Python
“Formatted String Literals.” 1) f-string nested in nested python ๋ฒ„์ „์ด ์˜ฌ๋ผ๊ฐ€๋ฉด์„œ ๋‹ค์–‘ํ•˜๊ณ  ํŽธ๋ฆฌํ•œ ๊ธฐ๋Šฅ์ด ๋งŽ์ด ์ƒ๊ฒผ๋‹ค. ๊ทธ ์ค‘ํ•˜๋‚˜์ธ f-string ์•ˆ์— ๋‹ค์‹œ f-string์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด๋‹ค. ์ด๋ ‡๊ฒŒ ๋˜๋ฉด ์•ž์„œ ์†Œ๊ฐœํ•œ f-string trick(1)์˜ datetime์„ ์ž์œ ์ž์žฌ๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. from datetime import datetime now : datetime = datetime.now() date_spec : str = "%d.%m.%Y" date = now | date_spec print(f"{now:{date_spec}}") # '17.03.2024' 2) file path file path๋ฅผ ๋ฌธ์ž์—ด๋กœ ์ฒ˜๋ฆฌํ•  ๋•Œ escape ..
[Python] f-string trick (1)
ยท
๐Ÿ Python
Python f-string์„ ํ™œ์šฉํ•œ ๊น”๋”ํ•œ ์ถœ๋ ฅ 1) int ๋ฌธ์ž์—ด ์ถœ๋ ฅ f-string์œผ๋กœ ์ˆซ์ž๋ฅผ ๊ฐ€๋…์„ฑ์ด ์ข‹์€ ํ˜•ํƒœ๋กœ ๊ตฌ๋ถ„์ž ์„ ํƒ์„ ํ•  ์ˆ˜ ์žˆ๋‹ค. n : int = 1000000 print(f"{n}") # 1000000 print(f"{n:_}") # 1_000_000 print(f"{n:,}") # 1,000,000 2) ๋ฌธ์ž์—ด ์ถœ๋ ฅ blank space ์ฑ„์šฐ๊ธฐ ํŠน์ • ํ•ญ๋ชฉ์„ ์ถœ๋ ฅํ•  ๋•Œ ๋ฌธ์ž์—ด์˜ ๊ธธ์ด๊ฐ€ ๋‹ฌ๋ผ ๋ณด๊ธฐํž˜๋“  ๊ฒฝ์šฐ ์ผ์ผํžˆ ๋ฌธ์ž์—ด์˜ ํฌ๊ธฐ๋งŒํผ ๋นˆ์นธ์„ ์‚ฌ์šฉํ•ด์•ผํ•œ๋‹ค. ์ด ๊ฒฝ์šฐ f-string์œผ๋กœ ๋ณด์ •ํ•  ์ˆ˜์žˆ๋‹ค. type1 : str = 'color' type2 : str = 'size' print(f"{type1:
[Version] 3.12 rc (Release Candidates) (1) - f-string
ยท
๐Ÿ Python
Python 3.11 -> 3.12 ๋ณ€ํ™” 1. Nested quote character f-string ์‚ฌ์šฉ์‹œ ์ž‘์€ ๋”ฐ์˜ดํ‘œ(')๋กœ dictionary์˜ item์— ์ ‘๊ทผ์ด ๊ฐ€๋Šฅํ•˜์ง€๋งŒ ํฐ ๋”ฐ์˜ดํ‘œ(")๋กœ๋Š” Error๊ฐ€ ๋‚œ๋‹ค. [3.11] fruit = { "name" : "apple", "price" : 3000 } f"{fruit['name']}" # apple f"{fruit["name"]}" # Syntax Error [3.12] fruit = { "name" : "apple", "price" : 3000 } f"{fruit['name']}" # apple f"{fruit["name"]}" # apple 2. f-string ์•ˆ์— ์ฃผ์„ ์‚ฌ์šฉ [3.11] fruit = { "name" : "apple", ..
๋‹คํ–ˆ๋‹ค
'f-string' ํƒœ๊ทธ์˜ ๊ธ€ ๋ชฉ๋ก