Python/Python Basic Lesson

Python Programming Basic - 2. 자료형 변환

Pydole 2020. 3. 2. 20:24

 

int() : 정수 자료형로 변환

 

floatData = 1.2

intData = int(floatData)   # 소수부분은 버리고, 정수값만 리턴
print(type(intData))
print(intData)



<class 'int'>
1

* type() 함수는 자료형을 확인하는 내장함수 (Built-in)

 


 

float() : 실수 자료형로 변환

 

intData = 1

floatData = float(intData)   # 정수를 실수로 변환
print(type(floatData))
print(floatData)



<class 'float'>
1.0

 


 

str() : 문자열 자료형으로 변환

 

intData = 1
floatData = 1.2

strData1 = str(intData)       # 정수를 문자열로 변경
strData2 = str(floatData)     # 실수를 문자열로 변경

print(type(strData1))
print(type(strData2))
print(strData1)
print(strData2)



<class 'str'>
<class 'str'>
1
1.2