위치 기반 포매팅

Python 정규 표현식

Maria Eugenia Inzaugarat

Data scientist

문자열 포매팅이란?

  • 문자열 보간(String interpolation)

  • 미리 정의된 텍스트에 사용자 지정 문자열 또는 변수를 삽입합니다:

custom_string = "String formatting"
print(f"{custom_string} is a powerful technique")
String formatting is a powerful technique
  • 사용 사례:
    • 그래프 제목
    • 메시지 또는 오류 표시
    • 함수에 구문 전달
Python 정규 표현식

포매팅 방법

  • 위치 기반 포매팅
  • 포맷 문자열 리터럴
  • Template 메서드
Python 정규 표현식

위치 기반 포매팅

  • 플레이스홀더를 값으로 대체

  • str.format()

 

print("Machine learning provides {} the ability to learn {}".format("systems", "automatically"))
Machine learning provides systems the ability to learn automatically
Python 정규 표현식

위치 기반 포매팅

  • 초기 문자열과 메서드에 전달되는 값 모두에 변수를 사용합니다
my_string = "{} rely on {} datasets"
method = "Supervised algorithms"
condition = "labeled"
print(my_string.format(method, condition))
Supervised algorithms rely on labeled datasets
Python 정규 표현식

값 재정렬하기

  • 플레이스홀더에 인덱스 번호를 포함해 값의 순서를 바꿀 수 있습니다
print("{} has a friend called {} and a sister called {}".format("Betty", "Linda", "Daisy"))
Betty has a friend called Linda and a sister called Daisy

 

print("{2} has a friend called {0} and a sister called {1}".format("Betty", "Linda", "Daisy"))
Daisy has a friend called Betty and a sister called Linda
Python 정규 표현식

이름이 지정된 플레이스홀더

  • 플레이스홀더에 이름을 지정합니다
tool="Unsupervised algorithms"
goal="patterns"
print("{title} try to find {aim} in the dataset".format(title=tool, aim=goal))
Unsupervised algorithms try to find patterns in the dataset
Python 정규 표현식

이름이 지정된 플레이스홀더

my_methods = {"tool": "Unsupervised algorithms", "goal": "patterns"}
print('{data[tool]} try to find {data[goal]} in the dataset'.format(data=my_methods))
Unsupervised algorithms try to find patterns in the dataset
Python 정규 표현식

포맷 지정자(Format specifier)

  • 사용할 데이터 유형을 지정합니다: {index:specifier}
print("Only {0:f}% of the {1} produced worldwide is {2}!".format(0.5155675, "data", "analyzed"))
Only 0.515568% of the data produced worldwide is analyzed!

 

print("Only {0:.2f}% of the {1} produced worldwide is {2}!".format(0.5155675, "data", "analyzed"))
Only 0.52% of the data produced worldwide is analyzed!
Python 정규 표현식

datetime 포매팅하기

 

from datetime import datetime
print(datetime.now())
datetime.datetime(2019, 4, 11, 20, 19, 22, 58582)

 

print("Today's date is {:%Y-%m-%d %H:%M}".format(datetime.now()))
Today's date is 2019-04-11 20:20
Python 정규 표현식

연습해 봅시다!

Python 정규 표현식

Preparing Video For Download...