How to Convert String to Number Python

To convert a string to a number, you can use the built-in int() function. For example:

num_str = "123"
num = int(num_str)
print(num)

This will output the number 123. If the string to be converted contains a floating point number, you can use the float() function.

float_str = "3.14"
float_num = float(float_str)
print(float_num)

This will output a floating point number of 3.14. If the string cannot be converted to a number, it will raise a ValueError exception.

bannerAds