Converting Strings to Numbers with int() and float()
str() converts any value into its string representation without modifying the original value
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
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?
Start with the value: The value 32 is an integer before conversion.
Call str(): str(32) creates a new string representing the integer.
Assign 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
Assuming that the original value changes after str() is called.
str() returns a new string and does not modify the original value.
Fix:
Store or use the returned string when text is needed, for example by assigning the result to another variable.Treating a number and its string representation as the same type.
They serve different purposes: integers are for math, while strings are for text.
Fix:
Track whether your program currently holds the original value or the string returned by str().Expecting str() to convert only numbers.
str() works on booleans, None, integers, floats, and other Python types.
Fix:
Remember that str() accepts any value and produces its string representation.Confusing the string result with the original value.
The result is a new string object.
Fix:
Use 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().
- 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.