What is Type Casting?

Type casting in Python means converting the data type of a value into another data type manually. For example, converting a string into an integer, or a float into an integer. This is useful when we want to perform certain operations or calculations that require specific data types.

Type casting is also called type conversion. Python automatically converts data types in some cases (called implicit type casting), but we can also do it manually (called explicit type casting).

There are 2 types of Type Casting in Python:
  • Explicit Type Casting
  • Implicit Type Casting

1. Explicit Type Casting

Explicit type casting means we manually convert the data type of a value from one type to another using built-in Python functions. It is the process where the programmer clearly defines which type the value should be converted to.

This is useful when we are taking input from users (which is usually in string format), and we need to perform calculations or comparisons using that input.

Functions used for Explicit Type Casting:

  1. Converts x to an integer

    This int(x) converts a value to an integer. If it's a float or numeric string, it removes the decimal part.

    πŸ§‘β€πŸ’» Example:

    print(int(5.99))
    print(int("10"))

    Output:

    5

    10

    πŸ“ Explanation: First value 5.99 is truncated to 5. Second value is a string "10", which is converted to integer 10.

  2. Converts x to a floating-point number

    This float(x) converts a value into a floating-point number.

    πŸ§‘β€πŸ’» Example:

    print(float(5))
    print(float("10.5"))

    Output:

    5.0

    10.5

    πŸ“ Explanation: Integer 5 becomes 5.0. String "10.5" is converted to float 10.5.

  3. Converts x to a string

    This str(x) converts any value into a string.

    πŸ§‘β€πŸ’» Example:

    print(str(123))
    print(str(True))

    Output:

    "123"

    "True"

    πŸ“ Explanation: The number 123 becomes a string. The boolean value True also becomes a string.

  4. Converts x to a boolean (True or False)

    bool(x) converts a value to True or False.

    • False: 0, 0.0, '', None, [], {}, etc.
    • True: All other values

    πŸ§‘β€πŸ’» Example:

    print(bool(0))
    print(bool("hello"))

    Output:

    False

    True

    πŸ“ Explanation: Zero is considered False. Non-empty string "hello" is True.

βœ… Summary Table for Explicit Type Casting

Function Purpose Example Output
int(x) Convert to integer int("10") 10
float(x) Convert to float float(5) 5.0
str(x) Convert to string str(123) "123"
bool(x) Convert to boolean bool("") False

2. Implicit Type Casting

Implicit type casting means Python automatically converts the data type of a value without any user involvement. Python does this when it feels the conversion is safe and does not lose information.

This usually happens when you are performing operations between different types. For example, if you add an integer and a float, Python automatically converts the integer to float so that no data is lost.

πŸ§‘β€πŸ’» Example:


  x = 5        # integer
  y = 2.5      # float
  z = x + y    # Python converts x to float automatically
  print(z)     # Output: 7.5
      

Why is Type Casting Important?

  • It helps in preventing type-related errors.
  • It allows us to work with different data types together.
  • We often need it when we take input from users (input is always string).

Common Mistakes in Type Casting:

  • Trying to convert non-numeric strings to integers or floats will give an error.
  • For example, int("abc") will raise a ValueError.

πŸ§‘β€πŸ’» Example:

# βœ… Correct
a = int("10")
                      
# ❌ Wrong (will give error)
b = int("ten")  # ValueError

Summary:

  • Type casting means converting one data type to another.
  • Explicit casting is done using functions like int(), float(), str(), etc.
  • Implicit casting is done automatically by Python during operations.
  • Always be careful while converting strings to numbers.

πŸ§‘β€πŸ’» Example:


  # Taking string input from user
  age_str = input("Enter your age: ")
  
  # Converting string to int
  age = int(age_str)
  
  # Displaying result
  print(f"You will be {age + 5} years old after 5 years.")
    

Welcome to ShikshaSanchar!

ShikshaSanchar is a simple and helpful learning platform made for students who feel stressed by exams, assignments, or confusing topics. Here, you can study with clarity and confidence.

Here, learning is made simple. Notes are written in easy English, filled with clear theory, code examples, outputs, and real-life explanations β€” designed especially for students like you who want to understand, not just memorize.

Whether you’re from school, college, or someone learning out of curiosity β€” this site is for you. We’re here to help you in your exams, daily studies, and even to build a strong base for your future.

Each note on this platform is carefully prepared to suit all levels β€” beginner to advanced. You’ll find topics explained step by step, just like a good teacher would do in class. And the best part? You can study at your pace, anytime, anywhere.

Happy Learning! – Team ShikshaSanchar