2775

How can I convert an str to a float?

"545.2222" -> 545.2222 

Or an str to a int?

"31" -> 31 

For the reverse, see Convert integer to string in Python and Converting a float to a string without rounding it.

Please instead use How can I read inputs as numbers? to close duplicate questions where OP received a string from user input and immediately wants to convert it, or was hoping for input (in 3.x) to convert the type automatically.

2
  • 20
    As a general rule, if you have an object in Python, and want to convert to that type of object, call type(my_object) on it. The result can usually be called as a function to do the conversion. For instance type(100) results in int, so you can call int(my_object) to try convert my_object to an integer. This doesn't always work, but is a good "first guess" when coding. Commented Jul 5, 2018 at 1:18
  • Also make sure the string is actually can be converted to float, one way of doing that is to write a custom function with a try/except block, that checks for return float(str_value) inside try scope. Commented Aug 6, 2023 at 23:20

33 Answers 33

1
2
-2

You can simply do this by

s = '542.22' f = float(s) # This converts string data to float data with a decimal point print(f) i = int(f) # This converts string data to integer data by just taking the whole number part of it print(i) 

For more information on parsing of data types check on python documentation!

Sign up to request clarification or add additional context in comments.

Comments

-2

If you want to change the type to some other data type, then you can use explicit type casting, by which I mean that you have to use int() for changing a string type into integer and float() to change it into float type.

But if we see into the concept of type casting we would realize that type casting is not a good choice as a programmer until it is necessary and hence we should use type casting only in cases where it is a serious requirement like when you use an input function for entering a user inputted value.

Bonus tip: you can also use type casting for changing tuples to lists and then convert them back into tuples and thus you can make changes in a tuple which is an immutable data type (list() and tuple() are the respective functions).

1 Comment

float() and friends are not type casts, they are type conversions, despite what you might read on low quality "how to Python" websites. And you are misunderstanding the reasons that say, C programmers should not use "until it is necessary". The normal, correct, Pythonic way to do what OP is after is simply float(<str>).
-2

These two functions can encode any string to a big number and vice versa

alphabet = '0123456789abcdefghijklmnopqrstuvwxyz' def string_to_int(string): intstring = [] for i in range(len(string)): n = int(string[i], 36) sn = str(n) if len(sn) == 1: intstring.append('0') intstring.append(sn) return int(''.join(intstring)) def int_to_string(integer): global alphabet string = str(integer) result = [] for i in range(0, len(string), 2): c1 = string[i] c2 = '' if len(string) >= i: c2 = string[i + 1] code = int(c1 + c2) result.append(alphabet[code]) return ''.join(result) 

Test them with this print

print(int_to_string(string_to_int('apple12345'))) 

1 Comment

This doesn't answer OP's question (which asks about floats) and there's never any reason to do this in Python. In fact, int() will convert any valid string representing an integer (I assume this is what you meant by 'any string') to the integer, and in fact your code fails on many cases where int() would give a result for a valid integer representation.
1
2

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.