MOCKSTACKS
EN
Questions And Answers

More Tutorials









Python Typecasting


Typecasting is the way to change one data type of any data or variable to another datatype, i.e., it changes the data type of any variable to some other data type.

I know it's a bit confusing but let me tell you in a simple manner. Suppose there is a string "34" Note: String is not integer since it is enclosed in double-quotes) and as we know, we can't add this to an integer number, let's say 6. But to do so, we can typecast this string to int data type, and then we can add 34+6 to get the output as 40. Have a look at the program below:

# Typecasting in Python :
abc = 5 
abc2 = '45'
abc3 = 55.95
xyz = 5.0

abc4=int(abc2)

print(abc+abc4) # Output : 50 
print(abc+int(abc2)) # Output : 50 

print(float(abc)+xyz) # It will add 5.0 + 5.0 and will return 10.0

print(str(abc)+45) # It will give an error as abc has been changed into string.


There are many functions to convert one data type into another type :

str() – this function allows us to convert some other data type into a string.

int() – this function allows us to convert some other data type into an integer. For example, str("34") returns 34 which is of type integer (int)

float() – this function allows us to convert some other data type into a floating-point number, i.e., a number with decimals.

input() Function – This function allows the user to receive input from the keyboard into the program as a string.

input() function always takes input as a string, i.e., if we ask the user to take a number as input, even then, it will take it as a string, and we will have to typecast it into another data type as per the use case.
If you enter 45 when the input() is called, you will get "45" as a string

# Input Function in Python:
print("Enter your name : ")
name = input() #It will take input from user
print(Your Name is",name) # It will show the name 
xyz = input(Enter your age : ")
print("Your age is ",xyz) # It will show your age 


Conclusion

In this page (written and validated by ) you learned about Python Typecasting . What's Next? If you are interested in completing Python tutorial, your next topic will be learning about: Python String Slicing.



Incorrect info or code snippet? We take very seriously the accuracy of the information provided on our website. We also make sure to test all snippets and examples provided for each section. If you find any incorrect information, please send us an email about the issue: mockstacks@gmail.com.


Share On:


Mockstacks was launched to help beginners learn programming languages; the site is optimized with no Ads as, Ads might slow down the performance. We also don't track any personal information; we also don't collect any kind of data unless the user provided us a corrected information. Almost all examples have been tested. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. By using Mockstacks.com, you agree to have read and accepted our terms of use, cookies and privacy policy.