๊ด€๋ฆฌ ๋ฉ”๋‰ด

IT’s Portfolio

[Python] ํŒŒ์ด์ฌ์˜ ๋ฌธ์ž์—ด ํฌ๋ฉ”ํŒ… ๋ณธ๋ฌธ

Development Study/Python

[Python] ํŒŒ์ด์ฌ์˜ ๋ฌธ์ž์—ด ํฌ๋ฉ”ํŒ…

f1r3_r41n 2020. 11. 29. 18:54
728x90
๋ฐ˜์‘ํ˜•

๐Ÿ“Œ ํŒŒ์ด์ฌ์˜ ๋ฌธ์ž์—ด ํฌ๋ฉ”ํŒ… ๋ฐฉ์‹

C์–ธ์–ด์™€ ๊ฐ™์€ % ๋ฌธ์ž์—ด ํฌ๋ฉ”ํŒ… ๋ฐฉ์‹

people = 5
height = 183.4
msg = "๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค."

print("ํ˜„์žฌ ์‚ฌ๋žŒ๋“ค์€ %d๋ช…์ด ์žˆ์Šต๋‹ˆ๋‹ค.\n๊ทธ ์ค‘ ํ•œ ๋ช…์˜ ํ‚ค๋Š” %.1fcm์ž…๋‹ˆ๋‹ค.\n%s" % (people, height, msg))
  1. %d => ์ •์ˆ˜ํ˜•

  2. %f => ์‹ค์ˆ˜ํ˜•

  3. %s => ๋ฌธ์ž์—ด

๊ฒฐ๊ณผ

 

Python์Šค๋Ÿฝ์ง€ ์•Š์€ C ๋ฐฉ์‹์ด๋ผ ํ•„์ž๋Š” ๋ณ„๋กœ ์„ ํ˜ธํ•˜์ง€ ์•Š๋Š”๋‹ค.

 

format ๋ฉ”์„œ๋“œ ๋ฐฉ์‹

name = "neicebee"

print("์ œ ์ด๋ฆ„์€ {}์ž…๋‹ˆ๋‹ค.".format(name))

๋ฌธ์ž์—ด ์ค‘ ์ง์ ‘ ์ง€์ •ํ•˜๊ณ  ์‹ถ์€ ์ž๋ฆฌ์— {}๋ฅผ ํฌํ•จํ•˜๊ณ  format ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋œ๋‹ค.

 

๊ฒฐ๊ณผ

 

ํ•„์ž๋Š” ๊ท€์ฐฎ์•„์„œ ์ž˜ ์•ˆ์“ด๋‹ค.

 

F-String ๋ฐฉ์‹

name = "neicebee"
money = 6000000

print(f"{name}๋‹˜์˜ ์€ํ–‰ ์ž”๊ณ ๋Š” {money:,}์› ์ž…๋‹ˆ๋‹ค.")

๋ฌธ์ž์—ด ์•ž์— f๋ฅผ ๋ถ™์ด๊ณ  format ๋ฐฉ์‹๊ณผ ๊ฐ™์ง€๋งŒ ์ค‘๊ด„ํ˜ธ {} ์•ˆ์— ๋ณ€์ˆ˜ ๋ช…์„ ์ง์ ‘ ์ž‘์„ฑํ•˜๋ฉด ๋œ๋‹ค.

 

๊ฒฐ๊ณผ

 

ํ•„์ž๋Š” ์ด ๋ฐฉ์‹์„ ์ž์ฃผ ์“ด๋‹ค. format ๋ฐฉ์‹๋ณด๋‹ค ์‚ฌ์šฉํ•˜๊ธฐ ์‰ฌ์šธ ๋ฟ๋งŒ ์•„๋‹ˆ๋ผ ์‘์šฉ๋„ ์‰ฝ๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.

728x90
๋ฐ˜์‘ํ˜•
Comments