Python/Python Basic Lesson

Python Programming Basic - 3. 입력/출력

Pydole 2020. 3. 2. 20:33

 

print 함수 : 괄호 안에 문자열 값을 출력

input 함수 : 사용자가 키보드로 입력 (Enter를 누을 때까지 대기)


문자열 출력

 

print("hello python")

 

 

숫자 출력

 

print(1)



1

 

 

 

수식의 결과값 출력

 

print(1+2)



3

 

 

 

변수값 출력

 

s = "hello python"
print(s)



hello python

 

 

 

(*) 연산자를 이용하여 N 횟수만큼 반복 출력

 

print('hello python ' * 2 )



hello python hello python 

 

 

 

빈줄만 출력하고 싶을 때

 

print()

 


 

문자열 변수에 입력하고, 출력하기

 

s = input()

# hello python 입력

print(s)



hello python

 

 

 

 

입력할 때, 문자열과 같이 출력하기

 

s = input('문자열 입력하세요:')

문자열 입력하세요:

print(s)



hello python

 

 

int( ), str( )을 이용하여 필요한 데이타 타입만 입력

 

s = int(input('숫자를 입력하세요:'))   # 입력받은 값을 숫자형으로 변경

숫자를 입력하세요:1



s = int(input('숫자를 입력하세요:'))   # 입력받은 값이 문자이면 숫자형 변경시 ValueError

숫자를 입력하세요:s

'''
ValueError                                Traceback (most recent call last)
<ipython-input-71-9e0b2f6ab385> in <module>
----> 1 s = int(input('숫자를 입력하세요:'))

ValueError: invalid literal for int() with base 10: 's'
'''