hhnn_soph
[Python] ํ์ด์ฌ print๋ฌธ(์ถ๋ ฅ๋ฌธ) ์ฌ์ฉํ๊ธฐ ๋ณธ๋ฌธ
๐ ํ์ด์ฌ์ ์ถ๋ ฅ๋ฌธ
1. ๊ธฐ๋ณธ ์ถ๋ ฅ
- print() ํจ์ ์ฌ์ฉ
: ๊ดํธ ์์ ์๋ฌด๊ฒ๋ ์์ฑํ์ง ์์ผ๋ฉด ํ ์ค ๋์ด์ฐ๊ธฐ๊ฐ ๋จ
: ๊ธฐ๋ณธ์ ์ผ๋ก ์ถ๋ ฅ๊ฐ ๋์ ๏ผผn(์ค๋ฐ๊ฟ)์ด ์ค์ ๋์ด ์์
- ๋ฌธ์์ด ์ถ๋ ฅ ์ ์ฑ๊ธ ์ฟผํ ์ด์ (' '), ๋๋ธ ์ฟผํ ์ด์ (" ") ๋ชจ๋ ์ฌ์ฉ ๊ฐ๋ฅ
print('Hello Python!')
print("Hello Python!")
print()
print("""Hello Python!""")
print('''Hello Python!''')
# ์ถ๋ ฅ ๊ฒฐ๊ณผ
Hello Python!
Hello Python!
Hello Python!
Hello Python!
2. ์ฌ๋ฌ ์ค ์ถ๋ ฅ
- ์ฑ๊ธ ์ฟผํ ์ด์ ํน์ ๋๋ธ ์ฟผํ ์ด์ 3๊ฐ(' ' ', " " ") ์ฌ์ฉ
- ํ ์ค์ฉ ๋์ด์ฐ๊ธฐ๊ฐ ๊ฐ๋ฅ(ex. hello3)
# ๋ฐฉ๋ฒ1
hello1 = '''Hello, world!
์๋
ํ์ธ์.
Python์
๋๋ค.'''
# ๋ฐฉ๋ฒ2
hello2 = \
'''
Hello, world!
์๋
ํ์ธ์.
Python์
๋๋ค.
'''
# ๋ฐฉ๋ฒ3
hello3 = """Hello, world!
์๋
ํ์ธ์.
Python์
๋๋ค."""
# ์ถ๋ ฅ ๊ฒฐ๊ณผ
# print(hello1) & print(hello2)
Hello, world!
์๋
ํ์ธ์.
Python์
๋๋ค.
# print(hello3)
Hello, world!
์๋
ํ์ธ์.
Python์
๋๋ค.
< ์ ์ฑ๊ธ ์ฟผํ ์ด์ , ๋๋ธ ์ฟผํ ์ด์ ๋ชจ๋ ์ฌ์ฉํ ๊น? >
- ๋ฌธ์์ด ์์ ์์๋ฐ์ดํ๋ ํฐ ๋ฐ์ดํ๋ฅผ ๋ฃ์ด์ผ ํ ๊ฒฝ์ฐ๊ฐ ์๊ธธ ๋! ํผ์ฉ ์ฌ์ฉ์ด ๊ฐ๋ฅ
- ๋จ, ๊ฐ์ ์ข ๋ฅ์ ์ฟผํ ์ด์ ์ผ๋ก ๋ฌถ์ง ์์
# ""๋ก ๋ฌถ๊ธฐ
print("'you'")
print("Python isn't difficult")
# ''๋ก ๋ฌถ๊ธฐ
print('"you"')
print('He said "Python is easy"')
# SyntaxError
print(''you'')
print('Python isn't difficult')
print("He said "Python is easy"")
# ์ถ๋ ฅ ๊ฒฐ๊ณผ
# ""๋ก ๋ฌถ๊ธฐ
'you'
Python isn't difficult
# ''๋ก ๋ฌถ๊ธฐ
"you"
He said "Python is easy"
# SyntaxError
print(''you'') ์๋ฌ
^
SyntaxError: invalid syntax
3. Separator ์ต์
sep=' ' ์์ ๊ฐ์ผ๋ก ๋ฌธ์๋ค์ ์ฐ๊ฒฐ์์ผ์ค
print('T', 'E', 'S', 'T', sep='')
print('T', 'E', 'S', 'T', sep=' ') # ํ์ดํธ์คํ์ด์ค
print('2019', '02', '19', sep='-')
print('niceman', 'google.com', sep='@')
# ์ถ๋ ฅ ๊ฒฐ๊ณผ
TEST
T E S T
2019-02-19
niceman@google.com
4. End ์ต์
- ์ค ๋ฐ๊ฟ ์์ด ๋ค์ ์ถ๋ ฅ๊ฐ์ ํ ์ค๋ก ์ถ๋ ฅ
- end=' ' ์ฌ์ด์ ํ์ดํธ ์คํ์ด์ค๋ฅผ ๋ฃ์ด์ฃผ๋ฉด ๋์ด์ฐ๊ธฐ๋ก ์ฐ๊ฒฐ๋จ
print('Welecome To', end=' ') # ํ์ดํธ์คํ์ด์ค
print('the black paradice', end=' ')
print('piano notes') # end ์ต์
๋ฏธ์ฌ์ฉ: ์ค๋ฐ๊พธ๊ธฐ
print('-The End-')
Welecome To the black paradice piano notes
-The End-
5. Format ์ฌ์ฉ
- .format() ํจ์ : { } ํ์ฉ
# ์์๋๋ก ๋์
print('{} and {}'.format('You', 'Me'))
# ๋ฒํธ๋๋ก ๋์
print("{0} and {1} and {0}".format('You', 'Me')) # You: 0, Me: 1
# ๋ณ์๋ก ์ง์ (๋ณด๋ค ์ ํ)
print("{a} are {b}".format(a='You', b='Good'))
# ์ถ๋ ฅ ๊ฒฐ๊ณผ
You and Me
You and Me and You
You are Good
- % s(๋ฌธ์) % d(์ ์), % f(์ค์) ํ์ฉ
- { }(์ค๊ดํธ) ์ฌ์ฉ์ ์์ : ๊ผญ ์์ฑํ๊ธฐ
- %() ์ฌ์ฉ ๊ฐ๋ฅ
# data type ์ง์ : ๋ณด๋ค ๋ ์ ํ
print("%s's favorite number is %d" %('Eunki', 7))
# ์ ์, ์์ ์ง์
print("Test1: %5d, Price: %4.2f" %(776, 6543.123))
print("Test2: {0: 5d}, Price: {1: 4.2f}".format(776, 6543.123))
print("Test3: {a: 5d}, Price: {b: 4.2f}".format(a=776, b=6543.123))
print("Test4: {: 5d}, Price: {: 4.2f}".format(776, 6543.123))
# ์ถ๋ ฅ ๊ฒฐ๊ณผ
Eunki's favorite number is 7
Test1: 776, Price: 6543.12
Test2: 776, Price: 6543.12
Test3: 776, Price: 6543.12
Test4: 776, Price: 6543.12
- %(์ซ์) d : (์ซ์) ์๋ฆฌ ์ ์
- %(์ซ์ 1).(์ซ์ 2) f : (์ซ์ 1) ์๋ฆฌ ์ ์, (์ซ์ 2) ์๋ฆฌ ์์
* format ํจ์์ ์์ธํ ์ฌ์ฉ๋ฒ์ ์ถํ ํฌ์คํ ์ ์ ๋ฆฌ!
6. Escape ์ฝ๋
- ํ๋ก๊ทธ๋๋ฐํ ๋ ์ฌ์ฉํ ์ ์๋๋ก ๋ฏธ๋ฆฌ ์ ์ํด ๋ "๋ฌธ์ ์กฐํฉ"
- ์ถ๋ ฅ๋ฌผ ์์ ' ํน์ "์ ํฌํจํ๊ฑฐ๋ ์ถ๋ ฅ๋ฌผ์ ๋ณด๊ธฐ ์ข๊ฒ ์ ๋ ฌํ๋ ์ฉ๋๋ก ์ฌ์ฉ
์ฝ๋ | ์ค๋ช | ์ฌ์ฉ๋น๋ | ์ฝ๋ | ์ค๋ช | ์ฌ์ฉ๋น๋ |
๏ผผn | ๋ฌธ์์ด ์์์ ์ค๋ฐ๊ฟ | ๋์ | ๏ผผr | ์บ๋ฆฌ์ง ๋ฆฌํด(์ค ๋ฐ๊ฟ ๋ฌธ์, ํ์ฌ ์ปค์๋ฅผ ๊ฐ์ฅ ์์ผ๋ก ์ด๋) |
๋ฎ์ |
๏ผผt | ๋ฌธ์์ด ์ฌ์ด ํญ ๊ฐ๊ฒฉ | ๋์ | ๏ผผf | ํผํผ๋(์ค ๋ฐ๊ฟ ๋ฌธ์, ํ์ฌ ์ปค์๋ฅผ ๋ค์์ค๋ก ์ด๋) |
๋ฎ์ |
๏ผผ๏ผผ | ๋ฌธ์ ๏ผผํํ | ๋์ | ๏ผผa | ๋ฒจ์๋ฆฌ(์ถ๋ ฅํ ๋ pc์์ ์๋ฆฌ๋จ) | ๋ฎ์ |
๏ผผ' | ๋ฌธ์ ' ํํ | ๋์ | ๏ผผb | ๋ฐฑ์คํ์ด์ค | ๋ฎ์ |
๏ผผ" | ๋ฌธ์ " ํํ | ๋์ | ๏ผผ000 | null๋ฌธ์ | ๋ฎ์ |
๊ทธ ์ธ ๋ค์ํ print ๊ตฌ๋ฌธ์ ์๊ณ ์ถ๋ค๋ฉด ์๋ ๋งํฌ์์ ํ์ธํ๊ธฐ!
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
'Language > Python' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Python] ํ์ด์ฌ ๋ฐ์ดํฐ ํ์ (์๋ฃํ) ์ข ๋ฅ (0) | 2021.09.11 |
---|---|
[Python] Simplejson ํจํค์ง ์ค์น ๋ฐ VScode ์ ์ฉ (0) | 2021.09.09 |
[Python] ํ์ด์ฌ ๊ฐ์ํ๊ฒฝ ์ค์ (0) | 2021.09.08 |
[Python] ํ์ด์ฌ ๋ฐ Visual Studio Code ์ค์น(ํ๊ฒฝ์ค์ ) (0) | 2021.09.08 |