Explicit Casting (int(), float(), str())0%
Common Pitfalls

Explicit Casting (int(), float(), str())

Beginner8 min readUpdated: 2026-09-03
Study Materials

Explicit Casting (int(), float(), str())

Explicit casting (type conversion) is manually initiated by the programmer using built-in conversion functions.


Key Concepts & Detailed Explanation

Explicit casting functions:

  • int(x): Converts to integer. Can parse string of digits or truncate float.
  • float(x): Converts integer or valid string to float.
  • str(x): Converts any Python object to its readable string representation.
  • list(seq) / tuple(seq) / set(seq): Converts between collections.

Code Examples & Output

Python
# Converting user input string to int and float
age_str = "21"
age = int(age_str)
print("Age next year:", age + 1)
 
price_str = "89.99"
price = float(price_str)
print("Total with tax:", price * 1.18)
 
# Number to string
order_id = 10429
message = "Order #" + str(order_id)
print(message)

Expected Output:

Output
Age next year: 22
Total with tax: 106.1882
Order #10429

Best Practices & Common Pitfalls

Always wrap string-to-number conversions in try-except blocks when handling user input.


Practice Quiz

1. What does float('123') return?

  • A) 123
  • B) 123.0
  • C) ValueError
  • D) None
Answer: B Explanation: float('123') converts the string to 123.0.

2. How do you convert the tuple (1, 2, 3) into a list?

  • A) list((1, 2, 3))
  • B) convert((1, 2, 3))
  • C) (1, 2, 3).toList()
  • D) to_list()
Answer: A Explanation: The list() constructor converts any iterable into a list.

Practice Challenge

Accept two float strings, cast them to integers, and print their sum.

Next Lesson

Common Pitfalls

Continue learning with hands-on practice, examples, and exercises in the upcoming topic.

Related Lessons

Previous LessonNext Lesson
Implicit CastingCommon Pitfalls

Practice Quiz

Test your understanding of this lesson with 1 questions. Each question has one correct answer.

PrevNext