String to int in Python

Overview
The conversion of one data type into the other data type is known as type casting or type conversion. To convert string to int, we use the int() method in python. To convert a string into a floating-point number, we use the float() method in python.
Transform Your Career
Choose from our industry-leading programs designed for career success
Modern Software and AI Engineering Program
Master full-stack development with AI integration
+1000 moreModern Data Science and ML with specialisation in AI
Advanced data science techniques with AI specialization
+1000 moreAdvanced AIML with Specialisation in Agentic AI
Deep dive into AIML with focus on Agentic systems
+1000 moreDevOps, Cloud & AI Platform Engineering
Build and manage AI-powered cloud infrastructure
+1000 moreAI Engineering Advanced Certification by IIT-Roorkee
Premier AI engineering certification from IIT-Roorkee
How to Convert String to int in Python?
The conversion of one data type into the other data type is known as type casting or type conversion. To convert string to int, we use the int() method in python. To convert a string into a floating-point number, we use the float() method in python.
The int in python is a built-in method that convert string to int. The int() method can convert a string or a number into an integer. We can also use int in python to convert binary numbers to decimal numbers, hexadecimal numbers to decimal numbers, and octal numbers into decimal numbers.
The int() method raises an error when a non-string or non-integer data type is given as a parameter. The built-in raised error is termed as TypeError.
Similarly, we can use the float() method to convert an integer or specific strings into floating-point numbers. The float() method takes string or integer data type and converts the given data into a floating-point number. If a string is passed which is not a decimal point number or strings mentioned above, then ValueError is raised.
Using int() Function
We can convert string to int using the Python's built-in method called the int() method. The int() method takes a string or integer data type and converts the given data into an integer number.
The syntax of the method, int in python is very simple:

The value parameter is required but the base parameter is optional.
The int in python converts string to int and returns the integer object value of the given string or floating number. If we do not give any parameter in the method, it will return 0.
The exception raised by int in python is known as TypeError.
Whenever we pass a data type other than a string or an integer, the int in python raises an error.
For example, if we try to the typecast list using int in python.
Example:
Let us convert a floating-point number into an integer using the method, int in python.
Output:
Read more about int in python
Turn Learning into Career Growth
Using float() Function
We can convert an integer or specific strings into floating-point numbers using the python built-in method called float() method. The float() method takes string or integer data type and converts the given data into a floating-point number.
The syntax of the method, int in python is very simple:
The value parameter is optional.
Example:
Let us convert an integer number into a floating-point number using the method, float in python.
Output:
Read more about float in python
Conclusion
- The conversion of one data type into the other data type is known as type casting or type conversion.
- To convert string to int, we use the int() method in python. To convert a string into a floating-point number, we use the float() method in python.
- The int() method raises an error when a non-string or non-integer data type is given as a parameter.
- If a string is passed which is not a decimal point number or strings mentioned above, then ValueError is raised.
Ready to take your Python skills to the next level? Enroll in our Python Course today and unlock the full potential of this versatile programming language.
Scaler Placement Report and Statistics
Scaler learners achieved 2.5x salary growth with average post-Scaler CTC reaching ₹23L.