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์ ์ด์ฉํ๋ฉด ์ ์ ๊ฑด๊ฐ์ ์ข๋ค๋ ๊ฒ์ ์ ์ ์๋ค.
๋ฐ์ํ
'๐ Python' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Python] f-string trick (2) (0) | 2024.03.17 |
---|---|
[Python] ์ด๊ฑฐ ๋ชจ๋ฅด๋ฉด ๋๋ ์ด๋ณด (0) | 2024.03.05 |
FastAPI - starlette.routing.NoMatchFound: No route exists for name (0) | 2024.01.19 |
[pip] WARNING: Skipping due to invalid metadata entry 'name' (0) | 2023.12.30 |
[Python] Command Line Flags (0) | 2023.12.26 |