hhnn_soph

[Python] ํŒŒ์ด์ฌ print๋ฌธ(์ถœ๋ ฅ๋ฌธ) ์‚ฌ์šฉํ•˜๊ธฐ ๋ณธ๋ฌธ

Language/Python

[Python] ํŒŒ์ด์ฌ print๋ฌธ(์ถœ๋ ฅ๋ฌธ) ์‚ฌ์šฉํ•˜๊ธฐ

hhnn_soph 2021. 9. 11. 03:01

๐Ÿ“™ ํŒŒ์ด์ฌ์˜ ์ถœ๋ ฅ๋ฌธ

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