Python Type Casting: Convert String to Integer with input()
This Snippet is coded by a user inOnline python Compiler. You can see and run this code too.
زبان: python
This Python code demonstrates type casting (converting a string to an integer) using the built-in int() function. It prompts the user to enter their age, stores it as a string from input(), then converts it to an integer before performing multiplication.
How it works:
input()reads user input as a string.int(age)converts the string to an integer.- The converted value is multiplied by 2 and printed.
This is essential when performing numeric operations on user input, as Python doesn't allow arithmetic with strings.