์ฝ”๋“œ - ํŒจ์บ  ์ˆ˜์—… ์ฝ”๋“œ ์ฐธ๊ณ  (ํŒจ์บ  ์ˆ˜์—… ์ •๋ฆฌ)

 

 

<์ฐธ๊ณ  ๋งํฌ>

https://www.python-course.eu/python3_formatted_output.php

 

Python Tutorial: Formatted Output

Even though it may look so, the formatting is not part of the print function. If you have a closer look at our examples, you will see that we passed a formatted string to the print function. Or to put it in other words: If the string modulo operator is app

www.python-course.eu

 

 

 

 

1. ๊ธฐ๋ณธ ์ถœ๋ ฅ 

print('Hello Python!')
print("Hello Python!")
print("""Hello Python!""")
print('''Hello Python!''')

 Hello Python! 
 Hello Python! 
 Hello Python! 
 Hello Python! 

์ž‘์€๋”ฐ์˜ดํ‘œ, ํฐ๋”ฐ์˜ดํ‘œ 1๊ฐœ ํ˜น์€ 3๊ฐœ๋กœ ๊ฐ์‹ธ์ค€๋‹ค. 

 

 

 

 

 

 

 

2. seperator ์˜ต์…˜ ์‚ฌ์šฉ 

print('T', 'E', 'S', 'T', sep='')
print('2019', '02', '19', sep='-')
print('niceman', 'google.com', sep="@")

 TEST 
 2019-02-19 
 niceman@google.com 

printํ•จ์ˆ˜์˜ ์ธ์ž์— sep= ์˜ต์…˜์„ ๋„ฃ์–ด์ฃผ๋ฉด sep์— ๋“ค์–ด๊ฐ€๋Š” ๋ฌธ์ž๊ฐ€ ์ค‘๊ฐ„์— ๋“ค์–ด ๊ฐ€ ๋ฌธ์ž์—ด์„ ์ด์–ด ์ค€๋‹ค. 

 

 

 

 

 

 

 

3. end ์˜ต์…˜ ์‚ฌ์šฉ 

print('Welcom To', end=' ')
print('the black prade', end=' ')
print('piano notes')

 Welcom To the black prade piano notes 

end์˜ต์…˜์€ defalut๊ฐ€ \n์œผ๋กœ ๋“ค์–ด ๊ฐ€ ์žˆ์–ด ์‚ฌ์šฉํ•˜์ง€ ์•Š์œผ๋ฉด ์ž๋™์œผ๋กœ ์ค„๋ฐ”๊ฟˆ์ด ๋์—ˆ๋‹ค. end=' ' ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์œ„์™€ ๊ฐ™์ด ๊ทธ ๋‹ค์Œ ์ค„์ด ํ˜ธ์ถœ๋  ๋•Œ ๋„์–ด ์“ฐ๊ธฐ ํ•œ ๋ฒˆ ํ›„ ์ถœ๋ ฅ์ด ๋œ๋‹ค. 

 

 

 

 

 

 

 

4. format ์‚ฌ์šฉ  { }. ( ) 

print('{} and {}'.format('You', 'Me'))
print("{0} and {1} and {0}".format('You', 'Me'))
print('{a} and {b}'.format(a='You', b='Me'))

 You and Me 
 You and Me and You 
 You and Me 

format ํ•จ์ˆ˜๋กœ ๋ฌธ์ž์—ด์— ๊ฐ’์„ ๋„ฃ์–ด์ค„ ์ˆ˜ ์žˆ๋‹ค. 

 

print("%s favorite number is %d" % ('Silver', 7))
print("Test1: %5d, Price: %4.2f" % (776, 6534.123))

 Silver favorite number is 7 
 Test1:   776, Price: 6534.12 

* %s : ๋ฌธ์ž, %d : ์ •์ˆ˜, %f : ์‹ค์ˆ˜

%๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๊ธ€์ž์ˆ˜๋‚˜, type์— ๋Œ€ํ•ด ๋ช…์‹œ์ ์œผ๋กœ ๊ธฐ์ž…ํ•ด ๋†“์„ ์ˆ˜ ์žˆ๋‹ค. 

 

print("Test1: {0: 5d}, Price: {1: 4.2f}".format(776, 6534.123))
print('Test1: {a: 5d}, Price: {b: 4.2f}'.format(a=776, b=6534.123))

 Test1:   776, Price:  6534.12 
 Test1:   776, Price:  6534.12 

์œ„์™€ ๊ฐ™์ด format์„ ์‚ฌ์šฉํ•˜์—ฌ ๊ธ€์ž์ˆ˜์™€ type์„ ์ง€์ •ํ•ด์ค„ ์ˆ˜๋„ ์žˆ๋‹ค. 

 

 

 

 

 

 

 

5. ์ด์Šค์ผ€์ดํ”„ ๋ฌธ์ž 

print("'you'")
print('\'you\'')
print('"you"')
print("""'you'""")
print('\\you\\\n')
print('\t\t\ttest')

 'you' 

์ข…๋ฅ˜๊ฐ€ ๋‹ค๋ฅธ ๋”ฐ์˜ดํ‘œ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์ด์ฒ˜๋Ÿผ ์•ˆ์— ์žˆ๋Š” ๋”ฐ์˜ดํ‘œ๋กœ ๋‚˜์˜ค๋„๋ก ํ•  ์ˆ˜ ์žˆ๋‹ค. 

 'you' 

์ข…๋ฅ˜๊ฐ€ ๊ฐ™์€ ๋”ฐ์˜ดํ‘œ๋ฅผ ์‚ฌ์šฉํ•˜๋ ค๋ฉด ์ด์Šค์ผ€์ดํ”„ ๋ฌธ์ž๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ถœ๋ ฅํ•œ๋‹ค. 

 "you" 
 'you' 

 \you\ 

                              
                        test 

\\, \n, \t ์„ ์‚ฌ์šฉํ•˜์—ฌ \, ์ค„๋ฐ”๊ฟˆ, ํƒญ์„ ์‹คํ–‰ํ•  ์ˆ˜๋„ ์žˆ๋”ฐ. 

 

 

* ์ด์Šค์ผ€์ดํ”„ ๋ฌธ์ž 

\n : ๊ฐœํ–‰

\t : ํƒญ

\\ : ๋ฌธ์ž

\' : ๋ฌธ์ž

\" : ๋ฌธ์ž

\r : ์บ๋ฆฌ์ง€ ๋ฆฌํ„ด

\f : ํผ ํ”ผ๋“œ

\a : ๋ฒจ ์†Œ๋ฆฌ

\b : ๋ฐฑ ์ŠคํŽ˜์ด์Šค

\000 : ๋„ ๋ฌธ์ž

...

 

 

 

 

 

 

 

 

+ Recent posts