728x90

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:<10}: ")
print(f"{type2:<10}: ")

# color     :
# size      :

 

3)  dateformat ์žฌ์ •์˜

datetime์„ ์›ํ•˜๋Š” format์œผ๋กœ ๋ฐ”๊พธ์–ด ์ถœ๋ ฅํ•˜๊ณ  ์‹ถ์„ ๋•Œ ๋ณ€์ˆ˜ ์„ ์–ธ ํ•˜์ง€ ์•Š๊ณ  ๋ฐ”๋กœ f-string ์ด์šฉ ๊ฐ€๋Šฅ

from datetime import datetime
now : datetime = datetime.now()

print(f"{now:%y.%m.%d (%H:%M:%S)}")
# 24.03.02 (13:53:58)

print(f"{now:%c}") # local version
# Sat Mar  2 13:55:28 2024

print(f"{now:%I%p}") # local version
# 01PM

 

Directive Meaning Example
%a Abbreviated weekday name. Sun, Mon, ...
%A Full weekday name. Sunday, Monday, ...
%w Weekday as a decimal number. 0, 1, ..., 6
%d Day of the month as a zero-padded decimal. 01, 02, ..., 31
%-d Day of the month as a decimal number. 1, 2, ..., 30
%b Abbreviated month name. Jan, Feb, ..., Dec
%B Full month name. January, February, ...
%m Month as a zero-padded decimal number. 01, 02, ..., 12
%-m Month as a decimal number. 1, 2, ..., 12
%y Year without century as a zero-padded decimal number. 00, 01, ..., 99
%-y Year without century as a decimal number. 0, 1, ..., 99
%Y Year with century as a decimal number. 2013, 2019 etc.
%H Hour (24-hour clock) as a zero-padded decimal number. 00, 01, ..., 23
%-H Hour (24-hour clock) as a decimal number. 0, 1, ..., 23
%I Hour (12-hour clock) as a zero-padded decimal number. 01, 02, ..., 12
%-I Hour (12-hour clock) as a decimal number. 1, 2, ... 12
%p Localeโ€™s AM or PM. AM, PM
%M Minute as a zero-padded decimal number. 00, 01, ..., 59
%-M Minute as a decimal number. 0, 1, ..., 59
%S Second as a zero-padded decimal number. 00, 01, ..., 59
%-S Second as a decimal number. 0, 1, ..., 59
%f Microsecond as a decimal number, zero-padded on the left. 000000 - 999999
%z UTC offset in the form +HHMM or -HHMM.  
%Z Time zone name.  
%j Day of the year as a zero-padded decimal number. 001, 002, ..., 366
%-j Day of the year as a decimal number. 1, 2, ..., 366
%U Week number of the year (Sunday as the first day of the week). All days in a new year preceding the first Sunday are considered to be in week 0. 00, 01, ..., 53
%W Week number of the year (Monday as the first day of the week). All days in a new year preceding the first Monday are considered to be in week 0. 00, 01, ..., 53
%c Localeโ€™s appropriate date and time representation. Mon Sep 30 07:06:05 2013
%x Localeโ€™s appropriate date representation. 09/30/13
%X Localeโ€™s appropriate time representation. 07:06:05
%% A literal '%' character. %

 

4)  ์—ฐ์‚ฐ print 

์ด์ „์—๋Š” ์—ฐ์‚ฐ์— ๋Œ€ํ•œ ๊ธฐํ˜ธ๋ฅผ ์“ฐ๊ณ  ํ”„๋ฆฐํŠธ ๋˜๋Š” ๊ฒฐ๊ณผ๋„ ์ถœ๋ ฅํ•ด์•ผํ–ˆ๋Š”๋ฐ ์ด์ œ๋Š” ํ•œ๋ฒˆ์— ๋ณ€์ˆ˜๋ช…์„ ๋”ฐ๋ผ ๋ฐ”๋กœ ์ถœ๋ ฅ๊ฐ€๋Šฅํ•˜๋‹ค. 

a : int = 5
b : int = 10

print(f"a + b = {a + b}")
# a + b = 15

print(f"{a + b = }")
# a + b = 15

 

 

 ์ด์ƒ f-string์„ ์ด์šฉํ•˜๋ฉด ์ •์‹  ๊ฑด๊ฐ•์— ์ข‹๋‹ค๋Š” ๊ฒƒ์„ ์•Œ ์ˆ˜ ์žˆ๋‹ค.

๋ฐ˜์‘ํ˜•
๋‹คํ–ˆ๋‹ค