강의

멘토링

커뮤니티

Inflearn Community Q&A

goffhahxh9480's profile image
goffhahxh9480

asked

[Kim Left-hand's Left-hand Coding] Ugly Coding Kid: Python Basics in 4 Hours (Including E-book)

Convert data types

my_int를 str로 바꾸고

Written on

·

224

0

'''my_int = 1'''

'''str(my_int)'''

까지 하고 my_int의 type 볼 때

왜 다시 type(str(my_int))를 해줘야 하는 거에요??

한번 문자열로 바꿨으면 type(my_int)를 해줬을 때 str로 나와야 하는 거 아닌가요??

이것도 규칙이라 그냥 따라야 되는 건가요?

python

Answer 1

0

저도 배우는 입장이라 맞는지는 모르겠습니다만,

str()의 경우 파라미터를 가공하여 string으로 리턴해주는 함수이므로, my_int 변수에는 아직 int형의 1이 저장되어있을 뿐, my_int에 str(my_int)를 다시 대입하지 않는 이상, int형 1이 유지될 것입니다.

따라서 type()함수에서 string 타입을 보시고 싶으시다면

my_int = 1

print(type(my_int))

my_int = str(my_int)

print(type(my_int))

이렇게 해보시면 str함수를 다시 호출하지 않아도 string 타입임을 확인하실 수 있으실 겁니다.

python 3.x에서 테스트해 보았습니다.

goffhahxh9480's profile image
goffhahxh9480

asked

Ask a question