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 |