Python Programming · Foundations
Type Conversion
On this page 9 sections
In 30 seconds
type conversion The explicit construction of a value of one Python type from an object of another type. Full entry → is making a value in one form into a value in another form. In Python, constructors such as int(), float(), str(), and bool() perform explicit conversions. The result depends on both the target type and the value supplied: int('42') produces the integer A whole-number value represented in Python by the int type. Full entry → 42, while int('3.5') raises ValueError An exception raised when an operation receives an argument of an acceptable type but an unsuitable value. Full entry → because that text is not an integer literal. Convert deliberately, then check what kind of value you received.
Why this matters
Programs often receive text but need a number, or need a number rendered as text. Explicit conversion makes that boundary visible, which helps you diagnose bad data instead of accidentally using the wrong kind of value. In coursework, it gives you a precise way to explain why an expression succeeds or raises an exception. Later lessons use the same habit with collections, files, and data-processing code: establish the shape and type of data before relying on it.
The college version
A constructor call asks for a target type
Every Python value has a type. A conversion call asks a built-in type to construct a value from the object supplied to it. For introductory code, the common conversions are int(), float(), str(), and bool(). int('42') returns the integer 42; float('3.5') returns the floating-point number A numeric value represented by the float type, commonly used for values with a fractional part. Full entry → 3.5; and str(19) returns the text string An ordered sequence of Unicode characters represented by Python's str type. Full entry → '19'. This is explicit conversion: the programmer writes the operation rather than expecting Python to silently reinterpret a value. The target type matters. str(7.0) preserves a textual representation of the float and returns '7.0', not the integer 7. Conversion is therefore not a generic cleanup command; it is a request for one particular type's documented construction rules. This lesson concentrates on those boundary-crossing calls. Arithmetic with numbers, string sequence operations, and the full truth-testing rules belong to their neighboring lessons.
Text must match the requested numeric form
When int() receives a string without an explicit base argument, the string must be a valid integer literal, allowing surrounding whitespace and a sign. Thus int(' -12 ') produces -12. A decimal point changes the situation: int('6.25') raises ValueError rather than quietly discarding .25. float() accepts appropriately formed numeric text such as float('6.25'), but float('not-a-number') also raises ValueError. These are predictable data-quality signals, not evidence that conversion is unreliable. A program that obtains text from a user, file, or service should decide what format it accepts and handle unsuitable input at the boundary. Do not use int(float(text)) as a casual workaround: it changes the meaning by first accepting a decimal value and then truncating it toward zero. If the input is meant to be a whole number, reject a decimal-looking string; if decimals are allowed, preserve the float or use a representation appropriate to the domain.
String and Boolean conversions have their own rules
str() makes a string representation of an object. It is useful when a program needs text for display, logging, or building a message, but it does not turn a number into a number-like string with a hidden numeric behavior; after str(19), the result is text. bool() constructs a Boolean A value of type bool, whose two possible values are True and False. Full entry → value according to truth-value testing. The key beginner surprise is that a nonempty string is truthy regardless of the letters inside it. bool('') is False because the string is empty, but bool('False') is True because it contains five characters. That is why bool() is not a parser for answers such as 'true', 'false', 'yes', and 'no'. Code that needs to interpret those words should normalize the text and compare it against an allowed vocabulary; the Booleans lesson develops truth testing more fully. Another related detail is that bool is a subclass of int, but treating True and False as ordinary numeric labels makes code harder to read. Use Boolean values to express conditions, not as a shortcut for arithmetic.
Make conversion boundaries easy to inspect
A dependable pattern is to keep raw input, convert it once, and then use the converted value. Suppose a program receives the text '18'. age_text = '18' preserves what arrived, and age = int(age_text) establishes that the next steps expect an integer. During development, type(age).__name__ can confirm the result is 'int'. If the input is '18.0', the same int() call raises ValueError, making the mismatch visible at exactly the conversion boundary. That is preferable to allowing a later calculation to fail mysteriously. It also lets tests state the intended input contract plainly. In production code, pair a narrowly scoped conversion attempt with a clear response appropriate to the program; exception handling is covered separately. The design principle is simple: know the accepted format, request the target type explicitly, and do not claim a conversion succeeded when Python raised an error. Conversion produces a new value; it does not mutate a string in place or retroactively change the source object.

Eli explains
The same idea, in plain words
Explain it like I’m 10
Think of a Python value as something written on a card. A card with 42 written as text is not automatically the same as a card holding the number 42. int('42') asks Python to read the text card as a whole number; str(42) asks it to make a text card from the number. Python follows rules for each request. If you ask for a whole number from '3.5', it refuses because a decimal is not written as an integer. That refusal is useful: it prevents Python from guessing what you meant.
Picture it like this
A conversion is like choosing a labeled measuring cup. A cup marked int accepts whole units; a cup marked float can represent a decimal amount; a cup marked str writes a label. Putting the wrong material into a cup does not make it fit. A label saying 'False' still has ink on it, so it is not an empty label—just as bool('False') is True because the string is nonempty.
Where the picture stops working
Python types are not physical containers, and bool() has more rules than checking whether a label has ink. The analogy also does not mean int(), float(), and str() can convert every possible object. Each constructor has documented rules and may raise an exception.
Worked example
A form supplies count_text = ' -12 ' and price_text = '6.25'. First, count = int(count_text) returns -12, an int; surrounding whitespace and the sign are allowed in an integer string. Next, price = float(price_text) returns 6.25, a float. For a message, label = 'Count: ' + str(count) returns 'Count: -12'; str() supplies text so concatenation is well-defined. Now consider int(price_text). Python raises ValueError because '6.25' is not an integer literal. The right fix depends on the data contract: if counts must be whole numbers, reject that input; if decimal quantities are intended, retain float(price_text). Finally, bool('False') is True—not because Python read the word as a logical value, but because the string is nonempty. Each result and error in this trace was executed with python3.
Key takeaway
Use int(), float(), str(), and bool() as explicit, rule-governed requests for a target type. Check the input format and the resulting type: a clear ValueError is safer than a silent guess.
Quick check
3 questions here, of 5 in this lesson’s practice set. Answers stay hidden until you check.
What does
Why does int('6.25') raise ValueError?
A program needs text to build the message 'Score: 19' from the integer 19. Which conversion is appropriate for the number?
Study tools & related lessonsYou’ll learn to · Common mistakes · Easily confused · Key vocabulary · Related
You’ll learn to
- Define explicit type conversion and name four common Python conversion constructors.
- Predict the result and resulting type of straightforward int(), float(), str(), and bool() calls.
- Distinguish a successful conversion from a ValueError caused by unsuitable text.
- Apply a safe conversion sequence to a small text-data example.
- Explain why bool('False') is True rather than interpreting the word False.
Common mistakes
Expecting int('3.5') to return 3.
It raises ValueError because the string is not an integer literal. Decide whether decimal input should be rejected or converted to float first for a deliberately different operation.
Using bool('False') to parse a written yes/no answer.
bool() tests whether the string is empty; any nonempty string is True. Parse allowed text values explicitly when that is the requirement.
Assuming str(19) leaves a number ready for arithmetic.
str(19) returns text. Convert back with a suitable numeric constructor before numeric work.
Ignoring a conversion error and guessing a fallback value.
Treat ValueError as a signal that the input failed the expected format; report or handle that mismatch at the boundary.
Easily confused
int('42') vs. float('42')
Both convert numeric text, but the first returns the integer 42 and the second returns the float 42.0.
bool('') vs. bool('False')
The empty string converts to False; the word 'False' is nonempty and therefore converts to True.
ValueError vs. successful conversion
A successful conversion produces the requested type; ValueError says the supplied value did not satisfy that constructor's required format.
Key vocabulary
- type conversion
- The explicit construction of a value of one Python type from an object of another type.
- type constructor
- A callable type such as int or str that creates an instance of that type according to its rules.
- integer
- A whole-number value represented in Python by the int type.
- floating-point number
- A numeric value represented by the float type, commonly used for values with a fractional part.
- string
- An ordered sequence of Unicode characters represented by Python's str type.
- Boolean
- A value of type bool, whose two possible values are True and False.
- ValueError
- An exception raised when an operation receives an argument of an acceptable type but an unsuitable value.
Sources & references
- Built-in Functions — Python 3 documentation — Python Software Foundation
- Built-in Types — Python 3 documentation — Python Software Foundation
EliExplains lessons are original prose written from the open, credible references above. See Copyright & Licensing.
Researched 2026-08-19
Educational content only. It is not medical, legal or professional advice. Found an error? Tell us.

