유니티 최고

숫자를 문자로, 문자를 숫자로 변환하는 방법 [총.정.리]

군포망나니 2022. 9. 1. 17:18

1. 숫자를 문자로 변환하는 방법 - 3가지

1.1 - str( ) 

      -> num = 7 (당연히 type은 int형 )

      -> num = str(num) (type이 str로 변환) 

1.2 - format() 

       -> num = 7 (당연히 type은 int형 )

       -> num = "{}".format(num) (type이 str로 변환) 

1.3 - f-string 

       ->  num = 7 ( 당연히 type은 int형 )

       -> num = f '{num}' (type이 str로 변환) 

참고 : https://codechacha.com/ko/python-convert-integer-to-string/

2. 문자를 숫자로 변환하는 방법 - 3가지 

2.1 -  ord() 

      -> s = "hi"

      -> s_list = []

      -> for i in s : s_list.append(ord(s)) 

      -> print(s) : [104,105]

2.2 - list 컴프리헨션 

      -> s = "hi" 

      -> s = [ord(x) for x in s] 

      -> print(s) : [104, 105]

참고  : https://www.delftstack.com/ko/howto/python/convert-letter-to-number-python/