Cross_Column

Saturday, 28 February 2026

Type Casting, Input/Output in Python



Python Type Casting and Input/Output Explained

Understanding Type Casting and Input/Output in Python

1. Type Casting in Python

Type casting means converting one data type into another data type. Python provides built-in functions to perform type conversion easily.

Common Type Casting Functions

  • int() – Converts value to integer
  • float() – Converts value to float
  • str() – Converts value to string
  • bool() – Converts value to boolean

Example 1: String to Integer

age = "25"
converted_age = int(age)
print(converted_age)
print(type(converted_age))
    

Example 2: Integer to Float

number = 10
float_number = float(number)
print(float_number)
print(type(float_number))
    
Important: If the string contains non-numeric characters, converting to int() will cause an error.

2. Input and Output in Python

Input and Output operations allow interaction between the user and the program.

Output in Python

The print() function is used to display output.

print("Hello, World!")
name = "Chandan"
print("My name is", name)
    

Input in Python

The input() function is used to take user input. By default, input is always stored as a string.

name = input("Enter your name: ")
print("Welcome", name)
    

Using Type Casting with Input

age = input("Enter your age: ")
age = int(age)
print("Next year you will be", age + 1)
    
Tip: Always convert input() to the required data type before performing calculations.

Conclusion

Type casting and Input/Output are fundamental concepts in Python. They are widely used in real-world applications, automation scripts, and Selenium testing frameworks.

No comments:

Post a Comment

Few More

Type Casting, Input/Output in Python

Python Type Casting and Input/Output Explained Understanding Type Casting and Input/Output in Python ...