Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How do I convert a string to a number in Python?
Python provides several built-in functions to convert strings to numbers. The most common methods are int() for integers and float() for decimal numbers.
Using int() for Integer Conversion
The int() function converts a string containing digits to an integer ?
# String to be converted
my_str = "200"
# Display the string and its type
print("String =", my_str)
print("Type =", type(my_str))
# Convert the string to integer using int()
my_int = int(my_str)
print("\nInteger =", my_int)
print("Type =", type(my_int))
String = 200 Type = <class 'str'> Integer = 200 Type = <class 'int'>
Using float() for Decimal Conversion
The float() function converts strings to floating-point numbers. You can also convert the float to integer if needed ?
# String to be converted
my_str = "500.75"
# Display the string and its type
print("String =", my_str)
print("Type =", type(my_str))
# Convert the string to float
my_float = float(my_str)
print("\nFloat =", my_float)
print("Type =", type(my_float))
# Convert the float to int (truncates decimal part)
my_int = int(my_float)
print("\nInteger =", my_int)
print("Type =", type(my_int))
String = 500.75 Type = <class 'str'> Float = 500.75 Type = <class 'float'> Integer = 500 Type = <class 'int'>
Converting Numbers with Different Bases
The int() function accepts a base parameter to convert strings representing numbers in different number systems ?
# String to be converted
my_str = "500"
print("String =", my_str)
print("Type =", type(my_str))
# Convert to base 10 (decimal)
base10_int = int(my_str)
print("\nInteger (base 10) =", base10_int)
# Convert to base 8 (octal) - treats string as octal
base8_int = int(my_str, base=8)
print("Integer (base 8) =", base8_int)
# Convert binary string
binary_str = "1010"
binary_int = int(binary_str, base=2)
print("\nBinary '1010' to decimal =", binary_int)
# Convert hexadecimal string
hex_str = "FF"
hex_int = int(hex_str, base=16)
print("Hexadecimal 'FF' to decimal =", hex_int)
String = 500 Type = <class 'str'> Integer (base 10) = 500 Integer (base 8) = 320 Binary '1010' to decimal = 10 Hexadecimal 'FF' to decimal = 255
Error Handling
Converting invalid strings will raise a ValueError. Use try-except blocks for safe conversion ?
invalid_strings = ["abc", "12.5", ""]
for string in invalid_strings:
try:
result = int(string)
print(f"'{string}' converted to {result}")
except ValueError:
print(f"Cannot convert '{string}' to integer")
Cannot convert 'abc' to integer Cannot convert '12.5' to integer Cannot convert '' to integer
Comparison
| Function | Purpose | Example Input | Result Type |
|---|---|---|---|
int() |
String to integer | "123" | int |
float() |
String to float | "123.45" | float |
int(s, base) |
String to integer with base | "FF", base=16 | int |
Conclusion
Use int() to convert string digits to integers, float() for decimal numbers, and specify the base parameter for different number systems. Always handle potential ValueError exceptions when converting user input.
