포맷 문자열 리터럴

Python 정규 표현식

Maria Eugenia Inzaugarat

Data Scientist

f-문자열

  • 최소한의 문법
  • 문자열 앞에 f 접두사 추가

way = "code"
method = "learning Python faster"
print(f"Practicing how to {way} is the best method for {method}")
Practicing how to code is the best method for learning Python faster
Python 정규 표현식

타입 변환

  • 허용되는 변환:
    • !s (문자열 버전)
    • !r (따옴표를 포함해 출력 가능한 표현을 담은 문자열)
    • !a (!r과 동일하지만 비 ASCII 문자를 이스케이프 처리)

 

name = "Python"

print(f"Python is called {name!r} due to a comedy series")
Python is called 'Python' due to a comedy series
Python 정규 표현식

형식 지정자

  • 표준 형식 지정자:
    • e (지수 표기법, 예: 5 10^3)
    • d (정수, 예: 4)
    • f (실수, 예: 4.5353)

 

number = 90.41890417471841

print(f"In the last 2 years, {number:.2f}% of the data was produced worldwide!")
In the last 2 years, 90.42% of the data was produced worldwide!
Python 정규 표현식

형식 지정자

 

  • datetime

 

from datetime import datetime
my_today = datetime.now()
print(f"Today's date is {my_today:%B %d, %Y}")
Today's date is April 14, 2019
Python 정규 표현식

인덱스 조회

family = {"dad": "John", "siblings": "Peter"}
print("Is your dad called {family[dad]}?".format(family=family))
Is your dad called John?

 

  • 인덱스 조회에는 따옴표를 사용하세요: family["dad"]
print(f"Is your dad called {family[dad]}?")
NameError: name 'dad' is not defined
Python 정규 표현식

이스케이프 시퀀스

  • 이스케이프 시퀀스: 백슬래시 \
print("My dad is called "John"")
SyntaxError: invalid syntax

 

my_string = "My dad is called \"John\""
My dad is called "John"
Python 정규 표현식

이스케이프 시퀀스

family = {"dad": "John", "siblings": "Peter"}
  • f-문자열에서는 백슬래시를 사용할 수 없습니다
print(f"Is your dad called {family[\"dad\"]}?")
SyntaxError: f-string expression part cannot include a backslash

 

print(f"Is your dad called {family['dad']}?")
Is your dad called John?
Python 정규 표현식

인라인 연산

  • 장점: 표현식을 평가하고 함수를 인라인으로 호출할 수 있음
my_number = 4
my_multiplier = 7
print(f'{my_number} multiplied by {my_multiplier} is {my_number * my_multiplier}')
4 multiplied by 7 is 28
Python 정규 표현식

함수 호출

def my_function(a, b):
  return a + b
print(f"If you sum up 10 and 20 the result is {my_function(10, 20)}")
If you sum up 10 and 20 the result is 30
Python 정규 표현식

연습해 봅시다!

Python 정규 표현식

Preparing Video For Download...