Converting Strings to Numbers with int() and float()
Numbers support mathematical work, while strings represent text.
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
- int(text) strips surrounding whitespace, allows one sign, then reads digits (underscores only between digits); any other character raises ValueError: invalid literal for int().
- int(text, base) reads digits in that base; base 0 takes the base from a 0x / 0o / 0b prefix like a Python literal.
- float(text) accepts decimals like '.5', '5.', '1e3' and the names inf, infinity and nan, then rounds to the nearest double.
- int() of a float truncates toward zero; str() gives the printed form (repr digits for floats).
- 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.
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
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.
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?
Reveal 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.
'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
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.
Saving the Converted Result
What does this assignment store in label?
- 1Start with the value
The value 32 is an integer before conversion.
- 2Call str()
str(32)creates a new string representing the integer. - 3Assign the result
The variable label receives the new string, while the original integer value is not modified.
label contains the string representation 32.
From Value to 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.
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 likescore = 32 followed by str(score)Why it failsstr()returns a new string and does not modify the original value.Do this insteadStore 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 likeThe integer 32 and the string representation 32 look alike when displayed.
Why it failsThey serve different purposes: integers are for math, while strings are for text.
Do this insteadTrack 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 likestr(True) or str(None)Why it failsstr()works on booleans, None, integers, floats, and other Python types.Do this insteadRemember that str() accepts any value and produces its string representation.
Common mistake Confusing the string result with the original value.
What it looks likestr(3.14159) produces the text 3.14159, not the original float itself.
Why it failsThe result is a new string object.
Do this insteadUse the returned result when text is required and keep the original value when its original type is still needed.
Practice the Trace
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().
- 1Check 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, andstr(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.