๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Python

[Python] ๋ฌธ์ž์—ด ํฌ๋งคํŒ…

by ์ฝ”๋”ฉํ•˜๋Š” ๋ถ•์–ด 2021. 2. 5.
๋ฐ˜์‘ํ˜•

๋ฌธ์ž์—ด ์•ˆ์˜ ํŠน์ •ํ•œ ๊ฐ’์„ ๋ฐ”๊ฟ”์•ผ ํ•  ๊ฒฝ์šฐ๊ฐ€ ์žˆ์„ ๋•Œ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ๋ฌธ์ž์—ด ํฌ๋งคํŒ… ๊ธฐ๋ฒ•์ด๋‹ค.

์‰ฝ๊ฒŒ ๋งํ•ด ๋ฌธ์ž์—ด ํฌ๋งคํŒ…์ด๋ž€ ๋ฌธ์ž์—ด ์•ˆ์— ์–ด๋–ค ๊ฐ’์„ ์‚ฝ์ž…ํ•˜๋Š” ๋ฐฉ๋ฒ•์ด๋‹ค.

 

 

 

โ–ถ ๋ฌธ์ž์—ด ํฌ๋งคํŒ… ๋”ฐ๋ผํ•˜๊ธฐ

-์ˆซ์ž ๋ฐ”๋กœ ๋Œ€์ž…

>>> "I eat %d apples." % 3
'I eat 3 apples.'

์—ฌ๊ธฐ์„œ %d๋Š” ๋ฌธ์ž์—ด ํฌ๋งท ์ฝ”๋“œ๋ผ๊ณ  ๋ถ€๋ฅธ๋‹ค.

 

 

-๋ฌธ์ž์—ด ๋ฐ”๋กœ ๋Œ€์ž…

>>> "I eat %s apples." % "five"
'I eat five apples.'

์ˆซ์ž๋ฅผ ๋„ฃ๊ธฐ ์œ„ํ•ด์„œ๋Š” %d๋ฅผ ์จ์•ผํ•˜๊ณ  ๋ฌธ์ž์—ด์„ ๋„ฃ๊ธฐ ์œ„ํ•ด์„œ๋Š” %s๋ฅผ ์จ์•ผํ•œ๋‹ค.

 

 

-์ˆซ์ž ๊ฐ’์„ ๋‚˜ํƒ€๋‚ด๋Š” ๋ณ€์ˆ˜๋กœ ๋Œ€์ž…

>>> number = 3
>>> "I eat %d apples." % number
'I eat 3 apples.'

 

 

-2๊ฐœ ์ด์ƒ์˜ ๊ฐ’ ๋„ฃ๊ธฐ

>>> number = 10
>>> day = "three"
>>> "I ate %d apples. so I was sick for %s days." % (number, day)
'I ate 10 apples. so I was sick for three days.'

 

 

 

โ–ถ ๋ฌธ์ž์—ด ํฌ๋งท ์ฝ”๋“œ

์ฝ”๋“œ ์„ค๋ช…
%s ๋ฌธ์ž์—ด(String)
%c ๋ฌธ์ž 1๊ฐœ(Character)
%d ์ •์ˆ˜(Integer)
%f ๋ถ€๋™ ์†Œ์ˆ˜(Floating-point)
%o 8์ง„์ˆ˜
%x 16์ง„์ˆ˜
%% Literal % (๋ฌธ์ž '%' ์ž์ฒด)

์—ฌ๊ธฐ์„œ %s ํฌ๋งท ์ฝ”๋“œ๋Š” ์–ด๋–ค ํ˜•ํƒœ์˜ ๊ฐ’์ด๋“  ๋ณ€ํ™˜ํ•ด ๋„ฃ์„ ์ˆ˜ ์žˆ๋‹ค.

>>> "I have %s apples" % 3
'I have 3 apples'
>>> "rate is %s" % 3.234
'rate is 3.234'

 

 

 

โ–ถ ํฌ๋งท ์ฝ”๋“œ์™€ ์ˆซ์ž ํ•จ๊ป˜ ์‚ฌ์šฉํ•˜๊ธฐ

-์ •๋ ฌ๊ณผ ๊ณต๋ฐฑ

>>> "%10s" % "hi"
'        hi'  ← hi๊ฐ€ ์˜ค๋ฅธ์ชฝ ์ •๋ ฌ๋จ

%10s๋Š” ์ „์ฒด ๊ธธ์ด๊ฐ€ 10๊ฐœ์ธ ๋ฌธ์ž์—ด ๊ณต๊ฐ„์—์„œ ๋Œ€์ž…๋˜๋Š” ๊ฐ’์„ ์˜ค๋ฅธ์ชฝ์œผ๋กœ ์ •๋ ฌํ•˜๊ณ  ๊ทธ ์•ž์˜ ๋‚˜๋จธ์ง€๋Š” ๊ณต๋ฐฑ์œผ๋กœ ๋‚จ๊ฒจ๋‘๋ผ๋Š” ์˜๋ฏธ์ด๋‹ค.

 

 

 

>>> "%-10sjane" % "hi"
'hi        jane'  ← hi๊ฐ€ ์™ผ์ชฝ ์ •๋ ฌ๋จ

๋ฐ˜๋Œ€์ชฝ์ธ ์™ผ์ชฝ ์ •๋ ฌ์€ %-10s๊ฐ€ ๋œ๋‹ค.

 

 

 

-์†Œ์ˆ˜์  ํ‘œํ˜„ํ•˜๊ธฐ

>>> "%0.4f" % 3.42134234
'3.4213'

3.42134234๋ฅผ ์†Œ์ˆ˜์  ๋„ค ๋ฒˆ์งธ ์ž๋ฆฌ๊นŒ์ง€๋งŒ ๋‚˜ํƒ€๋‚ด๊ณ  ์‹ถ์€ ๊ฒฝ์šฐ์—๋Š” ์œ„์™€ ๊ฐ™์ด ์‚ฌ์šฉํ•œ๋‹ค.

 

 

>>> "%10.4f" % 3.42134234
'    3.4213'

์ˆซ์ž 3.42134234๋ฅผ ์†Œ์ˆ˜์  ๋„ค ๋ฒˆ์งธ ์ž๋ฆฌ๊นŒ์ง€๋งŒ ํ‘œ์‹œํ•˜๊ณ  ์ „์ฒด ๊ธธ์ด๊ฐ€ 10๊ฐœ์ธ ๋ฌธ์ž์—ด ๊ณต๊ฐ„์—์„œ ์˜ค๋ฅธ์ชฝ์œผ๋กœ ์ •๋ ฌํ•œ๋‹ค.

 

 

 

โ–ถ format ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•œ ํฌ๋งคํŒ…

-์ˆซ์ž ๋ฐ”๋กœ ๋Œ€์ž…ํ•˜๊ธฐ

>>> "I eat {0} apples".format(3)
'I eat 3 apples'

 

 

-๋ฌธ์ž์—ด ๋ฐ”๋กœ ๋Œ€์ž…ํ•˜๊ธฐ

>>> "I eat {0} apples".format("five")
'I eat five apples'

 

 

-์ˆซ์ž ๊ฐ’์„ ๊ฐ€์ง„ ๋ณ€์ˆ˜๋กœ ๋Œ€์ž…ํ•˜๊ธฐ

>>> number = 3
>>> "I eat {0} apples."format(number)
'I eat 3 apples'

 

 

-2๊ฐœ ์ด์ƒ์˜ ๊ฐ’ ๋„ฃ๊ธฐ

>>> number = 10
>>> day = "three"
>>> "I ate {0} apples. so I was sick for {1} days.".format(number, day)
'I ate 10 apples. so I was sick for three days.'

 

 

-์ด๋ฆ„์œผ๋กœ ๋„ฃ๊ธฐ

>>> "I ate {number} apples. so I was sick for {day} days.".format(number=10, day=3)
'I ate 10 apples. so I was sick for 3 days.'

 

 

-์ธ๋ฑ์Šค์™€ ์ด๋ฆ„์„ ํ˜ผ์šฉํ•ด์„œ ๋„ฃ๊ธฐ

>>> "I ate {0} apples. so I was sick for {day} days.".format(10, day=3)
'I ate 10 apples. so I was sick for 3 days.'

 

 

-์™ผ์ชฝ ์ •๋ ฌ

>>> "{0:<10}".format("hi")
'hi        '

:<10 ํ‘œํ˜„์‹์„ ์‚ฌ์šฉํ•˜๋ฉด ์น˜ํ™˜๋˜๋Š” ๋ฌธ์ž์—ด์„ ์™ผ์ชฝ์œผ๋กœ ์ •๋ ฌํ•˜๊ณ  ๋ฌธ์ž์—ด์˜ ์ด ์ž๋ฆฟ์ˆ˜๋ฅผ 10์œผ๋กœ ๋งž์ถœ ์ˆ˜ ์žˆ๋‹ค.

 

 

-์˜ค๋ฅธ์ชฝ ์ •๋ ฌ

>>> "{0:>10}".format("hi")
'        hi'

์˜ค๋ฅธ์ชฝ ์ •๋ ฌ์€ :< ๋Œ€์‹  :>์„ ์‚ฌ์šฉํ•˜๋ฉด ๋œ๋‹ค.

 

 

-๊ฐ€์šด๋ฐ ์ •๋ ฌ

>>> "{0:^10}".format("hi")
'    hi    '

:^ ๊ธฐํ˜ธ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๊ฐ€์šด๋ฐ ์ •๋ ฌ๋„ ๊ฐ€๋Šฅํ•˜๋‹ค.

 

 

-๊ณต๋ฐฑ ์ฑ„์šฐ๊ธฐ

>>> "{0:=^10}".format("hi")
'====hi===='
>>> "{0:!<10}".format("hi")
'hi!!!!!!!!'

์ฑ„์›Œ ๋„ฃ์„ ๋ฌธ์ž ๊ฐ’์€ ์ •๋ ฌ ๋ฌธ์ž <, >, ^ ๋ฐ”๋กœ ์•ž์— ๋„ฃ์–ด์•ผ ํ•œ๋‹ค.

 

 

-์†Œ์ˆ˜์  ํ‘œํ˜„ํ•˜๊ธฐ

>>> y = 3.42134234
>>> "{0:0.4f}".format(y)
'3.4213'

 

 

-{ ๋˜๋Š” } ๋ฌธ์ž ํ‘œํ˜„ํ•˜๊ธฐ

>>> "{{ and }}".format()
'{ and }'

format ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•ด ๋ฌธ์ž์—ด ํฌ๋งคํŒ…์„ ํ•  ๊ฒฝ์šฐ { }์™€ ๊ฐ™์€ ์ค‘๊ด„ํ˜ธ ๋ฌธ์ž๋ฅผ ํฌ๋งคํŒ… ๋ฌธ์ž๊ฐ€ ์•„๋‹Œ ๋ฌธ์ž ๊ทธ๋Œ€๋กœ ์‚ฌ์šฉํ•˜๊ณ  ์‹ถ์€ ๊ฒฝ์šฐ์—๋Š” ์œ„ ์˜ˆ์˜ {{ }}์ฒ˜๋Ÿผ 2๊ฐœ๋ฅผ ์—ฐ์†ํ•ด์„œ ์‚ฌ์šฉํ•˜๋ฉด ๋œ๋‹ค.

 

 

-f ๋ฌธ์ž์—ด ํฌ๋งคํŒ…

>>> name = 'ํ™๊ธธ๋™'
>>> age = 30
>>> f'๋‚˜์˜ ์ด๋ฆ„์€ {name}์ž…๋‹ˆ๋‹ค. ๋‚˜์ด๋Š” {age}์ž…๋‹ˆ๋‹ค.'
'๋‚˜์˜ ์ด๋ฆ„์€ ํ™๊ธธ๋™์ž…๋‹ˆ๋‹ค. ๋‚˜์ด๋Š” 30์ž…๋‹ˆ๋‹ค.'

๋ฌธ์ž์—ด ์•ž์— f ์ ‘๋‘์‚ฌ๋ฅผ ๋ถ™์ด๋ฉด f ๋ฌธ์ž์—ด ํฌ๋งคํŒ… ๊ธฐ๋Šฅ์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

f ๋ฌธ์ž์—ด ํฌ๋งคํŒ…์€ ์œ„์™€ ๊ฐ™์ด name, age์™€ ๊ฐ™์€ ๋ณ€์ˆ˜ ๊ฐ’์„ ์ƒ์„ฑํ•œ ํ›„์— ๊ทธ ๊ฐ’์„ ์ฐธ์กฐํ•  ์ˆ˜ ์žˆ๋‹ค. ๋˜ํ•œ f ๋ฌธ์ž์—ด ํฌ๋งคํŒ…์€ ํ‘œํ˜„์‹์„ ์ง€์›ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๊ฒƒ๋„ ๊ฐ€๋Šฅํ•˜๋‹ค.

 

>>> age = 30
>>> f'๋‚˜๋Š” ๋‚ด๋…„์ด๋ฉด {age+1}์‚ด์ด ๋œ๋‹ค.'
'๋‚˜๋Š” ๋‚ด๋…„์ด๋ฉด 31์‚ด์ด ๋œ๋‹ค.'

 

 

-๋”•์…”๋„ˆ๋ฆฌ๋Š” f ๋ฌธ์ž์—ด ํฌ๋งคํŒ…์—์„œ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

>>> d = {'name':'ํ™๊ธธ๋™', 'age':30}
>>> f'๋‚˜์˜ ์ด๋ฆ„์€ {d["name"]}์ž…๋‹ˆ๋‹ค. ๋‚˜์ด๋Š” {d["age"]}์ž…๋‹ˆ๋‹ค.'
'๋‚˜์˜ ์ด๋ฆ„์€ ํ™๊ธธ๋™์ž…๋‹ˆ๋‹ค. ๋‚˜์ด๋Š” 30์ž…๋‹ˆ๋‹ค.'

 

 

-์ •๋ ฌ์€ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ํ•  ์ˆ˜ ์žˆ๋‹ค.

>>> f'{"hi":<10}'  ← ์™ผ์ชฝ ์ •๋ ฌ
'hi        '
>>> f'{"hi":>10}'  ← ์˜ค๋ฅธ์ชฝ ์ •๋ ฌ
'        hi'
>>> f'{"hi":^10}'  ← ๊ฐ€์šด๋ฐ ์ •๋ ฌ
'    hi    '

 

 

-๊ณต๋ฐฑ ์ฑ„์šฐ๊ธฐ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด ํ•  ์ˆ˜ ์žˆ๋‹ค.

>>> f'{"hi":=^10}'   ← ๊ฐ€์šด๋ฐ ์ •๋ ฌํ•˜๊ณ  = ๋ฌธ์ž๋กœ ๊ณต๋ฐฑ ์ฑ„์šฐ๊ธฐ
'====hi===='
>>> f'{"hi":!<10}'   ← ์™ผ์ชฝ ์ •๋ ฌํ•˜๊ณ  ! ๋ฌธ์ž๋กœ ๊ณต๋ฐฑ ์ฑ„์šฐ๊ธฐ
'hi!!!!!!!!'

 

 

-์†Œ์ˆ˜์ ์€ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ํ‘œํ˜„ํ•  ์ˆ˜ ์žˆ๋‹ค.

>>> y = 3.42134234
>>> f'{y:0.4f}'
'3.4213'
>>> f'{y:10.4f}'
'    3.4213'

 

 

-f ๋ฌธ์ž์—ด์—์„œ { } ๋ฌธ์ž๋ฅผ ํ‘œ์‹œํ•˜๋ ค๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋‘ ๊ฐœ๋ฅผ ๋™์‹œ์— ์‚ฌ์šฉํ•ด์•ผ ํ•œ๋‹ค.

>>> f'{{ and }}'
'{ and }'
๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€