์ฝ๋ - ํจ์บ ์์ ์ฝ๋ ์ฐธ๊ณ (ํจ์บ ์์ ์ ๋ฆฌ)
<์ฐธ๊ณ ๋งํฌ>
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 : ๋ ๋ฌธ์
...