📝 파이썬의 출력
🖥️ 1. print 함수
1-1. print()
- 매개변수로 지정된 데이터를 출력하고 줄을 변경
print('안녕하세요')
print("안녕하세요 파이썬")
[결과]
안녕하세요
안녕하세요 파이썬
print(10)
print(안녕)
[결과]
10
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-3-60eb73c3bb0b> in <cell line: 2>()
1 print(10)
----> 2 print(안녕)
NameError: name '안녕' is not defined
print('Hello', 'Python')
print('Hello', 'Python', "Hi", 'Python')
print('Hello', end = '')
print('Python')
print('Hello', end = ' ')
print('Python')
[결과]
Hello Python
Hello Python Hi Python
HelloPython
Hello Python
print(10 + 5)
print(10 - 5)
print(10 * 5)
print(10 / 5)
print(10 % 3)
print('10 + 5')
print('10')
print('10' + 5)
[결과]
15
5
50
2.0
1
10 + 5
10
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-15-428a61a39813> in <cell line: 8>()
6 print('10 + 5')
7 print('10')
----> 8 print('10' + 5)
TypeError: can only concatenate str (not "int") to str
1-2. 주석문
'''
여기는 여러줄 주석으로 처리합니다.
따라서 프로그램에 영항을 주지 않습니다.
'''
"""
이렇게 해도 여러줄 주석 가능
"""
🖥️ 2. 출력 서식 지정하기
- %d(정수), %f(실수), %s(문자열)를 사용하여 출력 서식을 지정
- 서식을 지정해 출력할 데이터가 2개 이상일 경우 ()로 데이터를 묶어야 한다.
print('숫자 : 100')
print('숫자 : %d' % 100)
print('숫자 : %d' % 100.12)
print('실수 : %f' % 100.12)
print('문자열 : %s' % 'Python')
print('문자열 : %s' % 10)
print('문자열 : %s' % 10)
print('%d + %d = %d' % (5, 3, 5+3))
[결과]
숫자 : 100
숫자 : 100
숫자 : 100
실수 : 100.120000
문자열 : Python
문자열 : 10
문자열 : 10
5 + 3 = 8
print('%5d' % 100)
[결과]
100
print('%5d' % 100)
print('%5d' % 10000)
print('%05d' % 100)
[결과]
100
10000
00100
print('%6.2f' % 123.45)
print('%6.2f' % 123.456)
print('%6.2f' % 123.4)
[결과]
123.45
123.46
123.40
print('%5s' % 'abc')
[결과]
abc
print('%d / %d = %d' % (5, 3, 5/3))
print('%d / %d = %f' % (5, 3, 5/3))
[결과]
5 / 3 = 1
5 / 3 = 1.666667
print('{0} {1}'.format('김자바', '이파이'))
print('{0}{1}'.format('김자바', '이파이'))
print('{1} {0}'.format('김자바', '이파이'))
[결과]
김자바 이파이
김자바이파이
이파이 김자바
print('{0:3s}님은 {1:03d}살입니다'.format('자바학생', 20))
print('{0}님은 {1:6.2f}cm입니다'.format('자바학생', 160.5))
[결과]
자바학생님은 020살입니다
자바학생님은 160.50cm입니다