본문 바로가기

프로그램&DB/Python

파이썬 int 정수형을 string 문자열로 변환하는 함수 How to Convert Int to String in Python

How to Convert Int to String in Python

 | updated May 22, 2011

When programming in Python, avoid "TypeErrors" by converting an integer to a string. For example, by converting numbers into strings you can easily align the results into a table. Another example is to concatenate a number before a string to enumerate an item. You use the "str" function to convert an integer to a string.

Instructions

    • 1

      Launch your Python editor.

    • 2

      Type "str (number)".

    • 3

      Press "Enter". This runs the string function to convert an integer to a string.

      In the example below, you prompt the user to enter an number. Use the "int" function to convert the number to an integer. Add five to the integer. Then, the "str" function converts the integer to a string so that Python can concatenate and print out the answer.

      "print ( "Enter an integer: ",)

      answer = input ()

      number = int (answer)

      addFive = number +5

      print ( "Adding 5 to your number, we get the answer " +str (addFive))"

      If you do not use "str" to convert the integer to a string, Python will give you the error "TypeError: cannot concatenate "str" and "int" objects".


'프로그램&DB > Python' 카테고리의 다른 글

파이썬 Unescape 함수 : Unescape HTML Entities in Python  (0) 2012.05.10
ASP에서 python 사용하기  (0) 2012.05.03
파이썬 메일 읽어오기  (0) 2012.04.09
파이썬 시작시 처리 사항  (0) 2012.04.09
날짜시간처리  (0) 2012.04.09