#1 Data Analytics Program in India
₹2,499₹1,499Enroll Now
4 min read

Type Conversion and Checking

Learn to change data types and check what type data is

What You'll Learn

  • What type conversion is
  • Converting between types
  • Checking data types
  • Avoiding common errors

What is Type Conversion?

Changing data from one type to another:

  • Number → Text
  • Text → Number
  • Number → True/False

Converting to String (Text)

Use str() to make anything into text:

code.py
# Number to string
age = 25
age_text = str(age)
print(age_text)  # "25"

# Use in sentences
message = "I am " + str(age) + " years old"
print(message)
# Shows: I am 25 years old
code.py
# Float to string
price = 19.99
price_text = str(price)
print(price_text)  # "19.99"

Converting to Integer (Whole Number)

Use int() to make whole numbers:

code.py
# String to integer
age_text = "25"
age = int(age_text)
print(age + 5)  # 30
code.py
# Float to integer (removes decimal)
price = 19.99
whole_price = int(price)
print(whole_price)  # 19

Warning: Text must look like a number!

code.py
# This works
num = int("123")  # 123

# This gives ERROR
num = int("hello")  # Error!

Converting to Float (Decimal Number)

Use float() to make decimal numbers:

code.py
# String to float
price_text = "19.99"
price = float(price_text)
print(price)  # 19.99
code.py
# Integer to float
age = 25
age_float = float(age)
print(age_float)  # 25.0

Converting to Boolean (True/False)

Use bool() to get True or False:

code.py
# Numbers
print(bool(1))      # True
print(bool(0))      # False
print(bool(-5))     # True
print(bool(100))    # True

# Strings
print(bool("Hello"))  # True
print(bool(""))       # False (empty = False)

# Lists
print(bool([1, 2]))   # True
print(bool([]))       # False (empty = False)

Rule: Empty = False, Everything else = True

Checking Data Types

Use type() to see what type something is:

code.py
age = 25
name = "John"
price = 19.99
is_student = True

print(type(age))        # <class 'int'>
print(type(name))       # <class 'str'>
print(type(price))      # <class 'float'>
print(type(is_student)) # <class 'bool'>

Practice Examples

Example 1: User input (always comes as text)

code.py
# Get age from user
age_text = input("Enter your age: ")

# Convert to number
age = int(age_text)

# Now you can do math
next_year = age + 1
print("Next year you will be:", next_year)

Example 2: Calculate with text numbers

code.py
num1_text = "10"
num2_text = "5"

# Convert to integers
num1 = int(num1_text)
num2 = int(num2_text)

# Now calculate
total = num1 + num2
print("Total:", total)
# Shows: Total: 15

Example 3: Format prices

code.py
price = 19.99

# Round to whole number
rounded = int(price)
print("Rounded price:", rounded)
# Shows: Rounded price: 19

Example 4: Check if empty

code.py
name = input("Enter name: ")

if bool(name):
    print("Hello", name)
else:
    print("You didn't enter a name!")

Common Conversions

code.py
# String to number
age = int("25")
price = float("19.99")

# Number to string
age_text = str(25)
price_text = str(19.99)

# To boolean
is_active = bool(1)      # True
is_empty = bool("")      # False

Safe Conversion

Check before converting to avoid errors:

code.py
text = "123"

# Check if it's a digit
if text.isdigit():
    number = int(text)
    print("Converted:", number)
else:
    print("Not a valid number!")

Common Mistakes

Mistake 1: Adding text and numbers

code.py
# Wrong
age = 25
print("Age: " + age)  # Error!

# Correct
print("Age: " + str(age))  # Age: 25

Mistake 2: Converting invalid text

code.py
# This causes error
number = int("hello")  # Error!

# Only convert number-like text
number = int("123")  # Works!

Mistake 3: Losing decimal precision

code.py
price = 19.99
whole = int(price)
print(whole)  # 19 (lost .99!)

Quick Reference

Convert to string:

code.py
str(value)

Convert to integer:

code.py
int(value)

Convert to float:

code.py
float(value)

Convert to boolean:

code.py
bool(value)

Check type:

code.py
type(value)

When to Use Each

Use str():

  • Combining with text
  • Displaying numbers

Use int():

  • Doing math with whole numbers
  • User input calculations

Use float():

  • Working with decimals
  • Precise calculations

Use bool():

  • Making decisions
  • Checking if empty

Summary

  • str() converts to text
  • int() converts to whole number
  • float() converts to decimal
  • bool() converts to True/False
  • type() checks what type data is
  • Always convert when mixing types

What's Next?

Now let's learn how to make decisions in code with if statements!

SkillsetMaster - AI, Web Development & Data Analytics Courses