I need to convert strings in Python to other types such as unsigned and signed 8, 16, 32, and 64 bit ints, doubles, floats, and strings.
How can I do this?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
You can convert a string to a 32-bit signed integer with the
intfunction:If the string does not represent an integer, you’ll get a
ValueErrorexception. Note, however, that if the string does represent an integer, but that integer does not fit into a 32-bit signed int, then you’ll actually get an object of typelonginstead.You can then convert it to other widths and signednesses with some simple math:
You can convert strings to floating point with the
floatfunction:Python floats are what other languages refer to as
double, i.e. they are 64-bits. There are no 32-bit floats in Python.