Examples
- Palindrome Program in Python (Check String is Palindrome or Not)
- Program to Remove Punctuations From String in Python
- Remove a Character from String in Python (Program With Examples)
- Remove Stop Words from String in Python Using NLTK and spaCy
- Program to Sort Words in Alphabetical Order in Python (Arrange in Alphabetic Order)
- How to Sort Strings in Python? (Program With Examples)
- How to Count Vowels in Python String? Vowels Program in Python
- How to Remove Vowels from String in Python? Program With Examples
- How to Convert String to Int or Float in Python? String Parse Program
- How to Convert Float to Int in Python? Program With Examples
- How to Convert Int to String in Python? Program with Examples
- Remove Spaces from String in Python (Trim Whitespace)
- Python Program to Check If Two Strings are Anagram
- How to Capitalize First Letter in Python? String Capitalization Program
- Find All Permutations of String in Python (Programs and Examples)
- Find All Substrings of a String in Python (Programs with Examples)
- Create Multiline String in Python (With & Without New Line)
How to Convert String to Int or Float in Python? String Parse Program
In Python, the skill to convert strings to numeric values, such as integers and floats, is a required for handling user input, data processing, and various numerical operations. In this tutorial, we will guide you on how to convert string to int in Python, as well as string to float.
Difference Between Int and Float in Python
Python differentiates between two main numeric data types: integers (int) and floating-point numbers (float).
The int represents whole numbers without any fractional or decimal parts, whereas a float represents numbers with fractional or decimal components.
The key distinction lies in the precision and range of values they can represent.
Example of Int in Python:
# Integer example
age = 25
print(age)
In this example, the variable age is assigned an integer value of 25. The value is not fractional or decimal, representing a whole number.
Example of Float in Python:
# Float example
pi = 3.14
print(pi)
In this example, the variable pi is assigned a float value of 3.14. The value includes a decimal point, indicating that it can represent fractional or decimal values.
Now that you have an idea of both integers and float, let's understand how to convert a string to int or float in Python programming. We will see programs and examples, as well as proper code explanations. You can practice the code on your own also, using the online Python compiler.
Program to Convert String to Int in Python (Str to int)
Here's a Python program to parse a string to an integer:
Code
def convert_to_int(string):
try:
converted_int = int(string)
return converted_int
except ValueError:
print("Error: Unable to convert the string to an integer.")
# Example usage
input_string = "345"
converted_number = convert_to_int(input_string)
print("<class 'int'>", converted_number)
Output
345
Explanation
In this program, we define a function called convert_to_int that takes a string as input. Inside the function, we attempt to convert the string to an integer using the int() function. If the conversion is successful, we return the converted integer.If the conversion fails due to a ValueError, we catch the exception and print an error message.
Program to Convert String to Float in Python
Here's a Python program to convert a string to a float:
Code
def convert_to_float(string):
try:
converted_float = float(string)
return converted_float
except ValueError:
print("Error: Unable to convert the string to a float.")
# Example usage
input_string = "3.14"
converted_number = convert_to_float(input_string)
print("<class 'float'>", converted_number)
Output
3.14
Explanation
We define a function called convert_to_float that takes a string as input. Inside the function, we attempt to convert the string to a float using the float() function. If the conversion is successful, we return the converted float.
If the conversion fails due to a ValueError, we catch the exception and print an error message.