Concepts / Converting Strings to Numbers with int() and float()

Converting Strings to Numbers with int() and float()

Numbers support mathematical work, while strings represent text.

  • Programming
Interactive lab

Try it: Type conversion: int(), float(), str(), bool()

Exactly how int(), int(text, base), float(), str() and bool() turn a value into another type: Python's real parsing rules for text (whitespace, sign, underscores, 0x/0o/0b prefixes, inf/nan, exponents) and its truthiness rules.

How it works

  1. int(text) strips surrounding whitespace, allows one sign, then reads digits (underscores only between digits); any other character raises ValueError: invalid literal for int().
  2. int(text, base) reads digits in that base; base 0 takes the base from a 0x / 0o / 0b prefix like a Python literal.
  3. float(text) accepts decimals like '.5', '5.', '1e3' and the names inf, infinity and nan, then rounds to the nearest double.
  4. int() of a float truncates toward zero; str() gives the printed form (repr digits for floats).
  5. bool() is truthiness: '', 0, 0.0 and None are False; every non-empty string — even '0' or 'False' — is True.

Default run (8 steps): int(' -0x_1F ', 0): the argument is a str of 10 characters. … int(' -0x_1F ', 0) → -31

Simplified: The argument is a Python literal typed as text (ASCII only; escapes \t \n \r). Unicode digits and bytes arguments are not modelled.

Educational simulation

Loading the simulation…

Why Values Become Text

Python uses different data types for different purposes. Numbers support mathematical work, while strings represent text. In a real program, however, a number may need to appear inside a message, be combined with other text, or be prepared for output or storage. The str() function bridges that gap by producing a string representation of a value.

What str() Returns

Definition

str() takes one value and returns a new string that represents that value. The original value is not modified.

When Python receives a value in str(), it examines that value and produces text according to the value's type. For an integer such as 32, the result contains the digits 32 as text. For a float such as 3.14159, the result includes the decimal point and digits. True becomes the string True, False becomes the string False, and None becomes the string None.

32integer3.14159float32string3.14159stringstr()str()
What changes, and what stays the same, when a value is passed to str()?

The diagram shows an important distinction: the displayed characters may look the same, but the type changes. The original integer 32 remains unchanged, while str(32) produces a separate string containing the characters 3 and 2.

Predicting Common Results

What do you think happens?

What string does str() produce for each of these values?

  • 32, 3.14159, True, and None in their displayed forms

  • Only the integer and float become strings

  • Every value becomes the string None

Reveal answer
Answer:

The results are the strings 32, 3.14159, True, and None.

The source material states that str() works with integers, floats, booleans, None, and other Python types. It represents each value as text without modifying the original value.

python
Output
'32'
'3.14159'
'True'
'None'

The quotation marks in the displayed results indicate strings in Python's representation of the results. The characters inside the quotes come from the original values. The conversion does not turn True into a number or change None itself; it creates text that represents each value.

Value Versus Text

32integer32string
How can the same displayed content differ when it is stored as a number versus as a string?

The two entries display the same visible characters, but they are different kinds of values. The first is the integer 32; the second is the string representation produced by str(32). Python treats these types differently because integers are used for mathematical purposes and strings are used for text.

Worked example

Saving the Converted Result

What does this assignment store in label?

  1. 1
    Start with the value

    The value 32 is an integer before conversion.

  2. 2
    Call str()

    str(32) creates a new string representing the integer.

  3. 3
    Assign the result

    The variable label receives the new string, while the original integer value is not modified.

Result

label contains the string representation 32.

python

From Value to Text

32integerstr()conversion function32stringpass valuereturns
How does a value move through str() and become text?

The conversion process has three useful parts. First, a program has a value such as an integer, float, boolean, or None. Second, that value is passed as the single argument to str(). Third, str() returns new text representing the value. That returned string can be assigned to a variable or used directly.

In the real world

Suppose a program is preparing a message for a user. A number may need to be represented as text so it can be included in that message. String conversion is also useful when preparing data for output, storage, or processing situations in which values need to be handled as text.

Mistakes with Conversion

  • Common mistake

    Assuming that the original value changes after str() is called.

    What it looks like
    score = 32 followed by str(score)
    Why it fails

    str() returns a new string and does not modify the original value.

    Do this instead

    Store or use the returned string when text is needed, for example by assigning the result to another variable.

  • Common mistake

    Treating a number and its string representation as the same type.

    What it looks like

    The integer 32 and the string representation 32 look alike when displayed.

    Why it fails

    They serve different purposes: integers are for math, while strings are for text.

    Do this instead

    Track whether your program currently holds the original value or the string returned by str().

  • Common mistake

    Expecting str() to convert only numbers.

    What it looks like
    str(True) or str(None)
    Why it fails

    str() works on booleans, None, integers, floats, and other Python types.

    Do this instead

    Remember that str() accepts any value and produces its string representation.

  • Common mistake

    Confusing the string result with the original value.

    What it looks like

    str(3.14159) produces the text 3.14159, not the original float itself.

    Why it fails

    The result is a new string object.

    Do this instead

    Use the returned result when text is required and keep the original value when its original type is still needed.

Practice the Trace

EASY

For each expression, predict the string returned by str(): str(7), str(2.5), str(False), and str(None). Then state whether the original value is changed.

Hints
  • Integers become their digits as text.
  • Floats retain their decimal point and digits in the representation described by the source material.
  • False and None become the corresponding text representations.
  • The original value is never modified by str().
  1. 1
    Check your reasoning against these results: str(7) produces the string 7, str(2.5) produces the string 2.5, str(False) produces the string False, and str(None) produces the string None. In every case, str() returns new text and leaves the original value unchanged.

Key Takeaways

  • str() takes one value and returns a new string representing that value.
  • It works with integers, floats, booleans, None, and other Python types.
  • The original value is not modified when str() is called.
  • A value and its string representation may display similar characters while remaining different types.
  • String conversion is useful when preparing values for text, messages, output, processing, or storage.

Related concepts

More concepts starting with C