Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Type Conversion Essentials | Cross-Type Interactions
Data Types in Python

bookType Conversion Essentials

Type conversion lets you move between core Python types so values can be compared, calculated, or displayed.

Converting to int

int(x) makes an integer.

  • From an int: returns the same number;
  • From a float: truncates toward zero (for example, int(2.9) returns 2, int(-2.9) returns -2);
  • From a string: the string must represent an integer (optional spaces and sign are ok).

Valid Conversions

12345678910
# Converting different types of user input to integers age_input = " 42 " temperature_reading = 2.9 negative_balance = -2.9 print(int(age_input)) # 42 β†’ clean string converted to int print(int(temperature_reading)) # 2 β†’ fractional part truncated print(int(negative_balance)) # -2 β†’ also truncates toward zero print(int("7")) # 7 β†’ string number becomes integer print(int(" -15 ")) # -15 β†’ handles spaces and sign
copy

These Raise ValueError

12
int("2.5") # ValueError - not an integer string int("42a") # ValueError
copy

Converting to float

float(x) makes a floating-point number.

  • Works for ints and decimal or scientific-notation strings;
  • Commas are not decimal points in Python.

Valid conversions

12345678
# Converting numeric inputs for a shopping calculator quantity = 3 price_str = "2.5" discount_factor = "1e3" # scientific notation for 1000 print(float(quantity)) # 3.0 β†’ integer converted to float print(float(price_str)) # 2.5 β†’ string price converted to float print(float(discount_factor)) # 1000.0 β†’ converts from scientific notation
copy

These Raise ValueError

1
float("2,5") # ValueError - use a dot, not a comma
copy

Converting to str

str(x) makes a human-readable string representation. Prefer f-strings when you are building messages.

12345678910
# Formatting a student's exam result student_age = 42 average_score = 3.5 print(str(student_age)) # "42" β†’ number converted to string print(str(average_score)) # "3.5" β†’ float converted to string student_name, final_score = "Ada", 98 report_message = f"{student_name} scored {final_score} points on the exam." print(report_message)
copy

Converting to bool

bool(x) follows Python truthiness rules.

  • Numbers: 0 is False, any other number is True;
  • Strings: "" (empty) is False, any non-empty string is True (even "0" and "False").
123456789101112
# Checking how different user inputs behave as boolean values login_attempts = 0 notifications = 7 username = "" user_id = "0" status = "False" print(bool(login_attempts)) # False β†’ 0 means no attempts yet print(bool(notifications)) # True β†’ non-zero means new notifications print(bool(username)) # False β†’ empty string means no username entered print(bool(user_id)) # True β†’ any non-empty string is truthy print(bool(status)) # True β†’ text "False" is still a non-empty string
copy

Mistakes to Avoid

  • int("2.5") raises ValueError - parse as float() first, then truncate or round;
  • Locale habit: "2,5" is invalid - use "2.5";
  • Underscores in input strings: "1_000" is invalid - remove underscores first: "1_000".replace("_", "");
  • Truthiness surprise: bool("0") is True - compare string contents explicitly if needed, for example s == "0".

1. What does each line produce?

2. Which call raises a ValueError?

3. Pick the correct statement.

question-icon

What does each line produce?

int(3.9) β†’
int(" -8 ")

bool("0")

Click or drag`n`drop items and fill in the blanks

question mark

Which call raises a ValueError?

Select the correct answer

question mark

Pick the correct statement.

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 4. ChapterΒ 1

Ask AI

expand

Ask AI

ChatGPT

Ask anything or try one of the suggested questions to begin our chat

Suggested prompts:

Can you explain more about how type conversion works in Python?

What happens if I try to convert a string that isn't a valid number?

Can you show examples of converting between other types, like lists or dictionaries?

Awesome!

Completion rate improved to 3.45

bookType Conversion Essentials

Swipe to show menu

Type conversion lets you move between core Python types so values can be compared, calculated, or displayed.

Converting to int

int(x) makes an integer.

  • From an int: returns the same number;
  • From a float: truncates toward zero (for example, int(2.9) returns 2, int(-2.9) returns -2);
  • From a string: the string must represent an integer (optional spaces and sign are ok).

Valid Conversions

12345678910
# Converting different types of user input to integers age_input = " 42 " temperature_reading = 2.9 negative_balance = -2.9 print(int(age_input)) # 42 β†’ clean string converted to int print(int(temperature_reading)) # 2 β†’ fractional part truncated print(int(negative_balance)) # -2 β†’ also truncates toward zero print(int("7")) # 7 β†’ string number becomes integer print(int(" -15 ")) # -15 β†’ handles spaces and sign
copy

These Raise ValueError

12
int("2.5") # ValueError - not an integer string int("42a") # ValueError
copy

Converting to float

float(x) makes a floating-point number.

  • Works for ints and decimal or scientific-notation strings;
  • Commas are not decimal points in Python.

Valid conversions

12345678
# Converting numeric inputs for a shopping calculator quantity = 3 price_str = "2.5" discount_factor = "1e3" # scientific notation for 1000 print(float(quantity)) # 3.0 β†’ integer converted to float print(float(price_str)) # 2.5 β†’ string price converted to float print(float(discount_factor)) # 1000.0 β†’ converts from scientific notation
copy

These Raise ValueError

1
float("2,5") # ValueError - use a dot, not a comma
copy

Converting to str

str(x) makes a human-readable string representation. Prefer f-strings when you are building messages.

12345678910
# Formatting a student's exam result student_age = 42 average_score = 3.5 print(str(student_age)) # "42" β†’ number converted to string print(str(average_score)) # "3.5" β†’ float converted to string student_name, final_score = "Ada", 98 report_message = f"{student_name} scored {final_score} points on the exam." print(report_message)
copy

Converting to bool

bool(x) follows Python truthiness rules.

  • Numbers: 0 is False, any other number is True;
  • Strings: "" (empty) is False, any non-empty string is True (even "0" and "False").
123456789101112
# Checking how different user inputs behave as boolean values login_attempts = 0 notifications = 7 username = "" user_id = "0" status = "False" print(bool(login_attempts)) # False β†’ 0 means no attempts yet print(bool(notifications)) # True β†’ non-zero means new notifications print(bool(username)) # False β†’ empty string means no username entered print(bool(user_id)) # True β†’ any non-empty string is truthy print(bool(status)) # True β†’ text "False" is still a non-empty string
copy

Mistakes to Avoid

  • int("2.5") raises ValueError - parse as float() first, then truncate or round;
  • Locale habit: "2,5" is invalid - use "2.5";
  • Underscores in input strings: "1_000" is invalid - remove underscores first: "1_000".replace("_", "");
  • Truthiness surprise: bool("0") is True - compare string contents explicitly if needed, for example s == "0".

1. What does each line produce?

2. Which call raises a ValueError?

3. Pick the correct statement.

question-icon

What does each line produce?

int(3.9) β†’
int(" -8 ")

bool("0")

Click or drag`n`drop items and fill in the blanks

question mark

Which call raises a ValueError?

Select the correct answer

question mark

Pick the correct statement.

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 4. ChapterΒ 1
some-alt