GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing www.geeksforgeeks.org — Type Casting in Python - GeeksforGeeks Type Casting in PythonLast Updated : 29 Jul, 2026 Type Casting is the method to convert the Python variable datatype into a certain data type in order to perform the required operation by users. We will see various techniques for typecasting. There can be two types of Type Casting in Python:
Example: The following code converts a numeric string into an integer so it can be used in arithmetic operations.
Output 26 <class 'int'> Implicit Type ConversionImplicit type conversion is the automatic changing of a data value from one type to another by the compiler or runtime environment without any manual intervention from the programmer
Output <class 'int'> <class 'float'> 10.0 <class 'float'> 21.0 <class 'float'> Explanation:
Explicit Type ConversionExplicit type conversion is when the programmer manually changes a value’s data type using built-in type casting functions, usually when automatic conversion is not possible or a specific type is needed. Examples of Type CastingCommonly used type casting functions in Python are:
Convert Int to FloatConverting Int to Float in with the float() function.
Output 5.0 <class 'float'> Python Convert Float to IntConverting Float to int datatype in Python with int() function.
Output 5 <class 'int'> Python Convert int to StringConverting int to String datatype in Python with str() function.
Output 5 <class 'str'> Python Convert String to floatCasting string data type into float data type with float() function.
Output 5.9 <class 'float'> Python Convert string to intConverting string to int datatype in Python with int() function. If the given string is not number, then it will throw an error.
Output
The string "5" is successfully converted into an integer 5. Since 't' is not a number, Python cannot convert it into an integer. Adding String and Integer Without ConversionIf we try to directly add an integer and a string, Python will throw an error because they are different data types.
Output
We should first convert the string into an integer and then add:
Output 12 <class 'int'> Comment Explore |
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |