Guidelines

format() ν•¨μˆ˜λ‘œ μ •μˆ˜ 좜λ ₯ν•˜κΈ°

format() ν•¨μˆ˜λ₯Ό μ‚¬μš©ν•˜λ©΄ λ¬Έμžμ—΄μ„ λ‹€μ–‘ν•œ ν˜•μ‹μœΌλ‘œ 좜λ ₯ν•  수 μžˆμŠ΅λ‹ˆλ‹€.

좜λ ₯ν•  λ°μ΄ν„°μ˜ ν˜•μ‹μ„ μ§€μ •ν•˜λŠ” 것을 ν¬λ§€νŒ…(formatting, ν˜•μ‹ν™”)이라고 ν•˜λ©°, μ€‘κ΄„ν˜Έ { } μ•ˆμ— :λ₯Ό μ‚¬μš©ν•˜μ—¬ 좜λ ₯ ν˜•μ‹μ„ μ§€μ •ν•©λ‹ˆλ‹€.


format() ν•¨μˆ˜ μ‚¬μš© μ˜ˆμ‹œ
"{:ν¬λ§·μ˜΅μ…˜}".format(κ°’)
  • { } : 자리 ν‘œμ‹œμžλ‘œ 값을 μ‚½μž…ν•˜λŠ” μœ„μΉ˜λ₯Ό 지정

  • : : 포맷의 μ˜΅μ…˜ 지정


μ •μˆ˜ 좜λ ₯ν•˜κΈ°

μ •μˆ˜λ₯Ό 좜λ ₯ν•  λ•ŒλŠ” {:d}와 같이 μ€‘κ΄„ν˜Έμ˜ : 였λ₯Έμͺ½μ— dλ₯Ό μ‚¬μš©ν•©λ‹ˆλ‹€.

μ •μˆ˜ 좜λ ₯ formatting μ˜ˆμ‹œ
number = 123 # μ •μˆ˜ 좜λ ₯ formatted = "number: {:d}".format(number) print(formatted) # "number: 123"

λ§Œμ•½ dλ₯Ό μƒλž΅ν•˜λ©΄, νŒŒμ΄μ¬μ€ κ°’μ˜ νƒ€μž…μ— 따라 μžλ™μœΌλ‘œ μ μ ˆν•œ 포맷을 μ‚¬μš©ν•©λ‹ˆλ‹€.

μžλ™ νƒ€μž… 지정 formatting μ˜ˆμ‹œ
number = 123 # μ •μˆ˜ 좜λ ₯ formatted = "number: {}".format(number) print(formatted) # "number: 123"

좜λ ₯ 폭 μ§€μ •ν•˜κΈ°

: 였λ₯Έμͺ½μ— 숫자λ₯Ό μ‚¬μš©ν•˜λ©΄ 좜λ ₯λ˜λŠ” λ¬Έμžμ—΄μ˜ 폭을 μ§€μ •ν•©λ‹ˆλ‹€.

예λ₯Ό λ“€μ–΄ {:5}λŠ” 좜λ ₯λ˜λŠ” λ¬Έμžμ—΄μ˜ 폭을 5둜 μ§€μ •ν•©λ‹ˆλ‹€.

μ •μˆ˜ 좜λ ₯ formatting μ˜ˆμ‹œ
number = 123 formatted = "number: {:5}".format(number) # 폭 5둜 κ³ μ • # 123 μ•žμ— 곡백 2κ°œκ°€ μ‚½μž…λ¨ print(formatted) # "number: 123",

λ§Œμ•½ 폭을 μ±„μšΈ λ•Œ 숫자 0을 μ‚¬μš©ν•˜κ³  μ‹Άλ‹€λ©΄, 폭 μ•žμ— 0을 λΆ™μž…λ‹ˆλ‹€.

μ •μˆ˜ 좜λ ₯ formatting μ˜ˆμ‹œ
number = 123 formatted = "number: {:05}".format(number) # 폭 5둜 κ³ μ • # 123 μ•žμ— 0이 2κ°œκ°€ μ‚½μž…λ¨ print(formatted) # "number: 00123"
Mission
0 / 1

format() ν•¨μˆ˜λ₯Ό μ‚¬μš©ν•˜μ—¬ μ •μˆ˜λ₯Ό κ³ μ •λœ 폭으둜 좜λ ₯ν•˜λ €λ©΄, μ–΄λ–€ 좜λ ₯ ν˜•μ‹μ„ μ‚¬μš©ν•΄μ•Ό ν• κΉŒμš”?

number = 123 formatted_number = "number: ".format(number) print(formatted_number) # "number: 123"
{:5}
{:.2f}
{:d}
{:s}

Guidelines

AI Tutor

Publish

Design

Upload

Notes

Favorites

Help

Code Editor

Run
Generate

Execution Result