Dev/Python 3

[Python] textwrap.dedent

멀티라인 문자열 (Multiline Strings)""" ... """ 멀티라인 문자열을 그대로 쓰면, 들여쓰기가 코드 전체 스타일과 어긋나서 보기가 안 예쁠 수 있음def main(): prompt = f"""When I use multi-line strings in Python,the indentation style looks misaligned and messy.How can I fix this?""" print(prompt)아래와 같이 코드 작성시 출력 앞부분에 공백이 들어감def main(): prompt = f""" When I use multi-line strings in Python, the indentation style looks misaligned and m..

Dev/Python 2025.09.18

Python f-string

Python f-string파이썬에는 %, .format(), f-string 등 여러 가지 문자열 포매팅 방식이 존재2025년 현재 대세이자 권장되는 방식은 f-string권장 이유:간결함: 변수와 표현식을 바로 중괄호 안에 넣을 수 있음가독성: 문자열과 코드가 뒤섞이지 않고 직관적성능: str.format() 보다 빠름 (CPython 구현 최적화 덕분)표현력: 수식 및 함수 호출도 직접 넣을 수 있음사용법:문자열 앞에 f (또는 F) 접두어 사용{} 내부에 변수명 또는 파이썬 표현식 작성포매팅 옵션 사용 가능중괄호 자체를 출력하려면 {{ 또는 }} 사용PEP 498 (Formatted String Literals) 에서 제안prompt = f"""Summarize the text delimited ..

Dev/Python 2025.08.23

Python dotenv 사용법 및 원리

사용 예시import osfrom dotenv import load_dotenvdef main(): load_dotenv(dotenv_path='.env') openai_api_key = os.getenv(key='OPENAI_API_KEY') print(openai_api_key)if __name__ == '__main__': main()동작 원리load_dotenv() 통해 변수를 현재 실행 중인 파이썬 프로세스의 환경 변수에 등록os.getenv() 통해 환경 변수 사용os.environ 통해 현재 실행 중인 파이썬 프로세스에 등록되어있는 환경 변수 확인 가능꼭 .env 파일이 아니어도 상관없지만 파일 내용은 KEY=VALUE 형식이어야 load_dotenv 가 환경 변수로 등..

Dev/Python 2025.08.17