String to int in Python

Learn via video course
FREE
View all courses
Python Course for Beginners With Certification: Mastering the Essentials
Python Course for Beginners With Certification: Mastering the Essentials
by Rahul Janghu
1000
4.90
Start Learning
Python Course for Beginners With Certification: Mastering the Essentials
Python Course for Beginners With Certification: Mastering the Essentials
by Rahul Janghu
1000
4.90
Start Learning
Topics Covered

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

NSDC Certified

Modern Software and AI Engineering Program

Master full-stack development with AI integration

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Modern Data Science and ML with specialisation in AI

Advanced data science techniques with AI specialization

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Advanced AIML with Specialisation in Agentic AI

Deep dive into AIML with focus on Agentic systems

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

DevOps, Cloud & AI Platform Engineering

Build and manage AI-powered cloud infrastructure

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

AI Engineering Advanced Certification by IIT-Roorkee

Premier AI engineering certification from IIT-Roorkee

3 MonthsDuration
AI-LedCurriculum
Career SupportSupport
Program highlights
Go to Program

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:

Using int function

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

1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary
1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary

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

₹23L
AVG CTC
SCALER PLACEMENT PROOF

Scaler learners achieved 2.5x salary growth with average post-Scaler CTC reaching ₹23L.

11,000+placements
650+companies
Verified data

See Also:

Hiring Partners:
GoogleGoogleAmazonAmazonMicrosoftMicrosoftFlipkartFlipkartAdobeAdobe1200+ more