f-string

· 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
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:
· 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' 태그의 글 목록