String Concatenation and Combining Text
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. Integers and floats represent numbers, while strings represent text. In a real program, you may need to display a number in a message, combine a value with other text, or prepare mixed data for output. The str() function bridges that gap by producing the string representation of a value.
String conversion changes how a value is represented for text-based work; it does not modify the original value.
After this conversion, score still holds the original integer value. The variable score_text holds a new string representing that value. The characters may look the same when displayed, but the integer and the string have different types and purposes.
What str() Preserves
The result of str() is a new string object. Assigning that result to another variable gives you a text version while leaving the value passed to str() unchanged. You can also use the returned string directly wherever text is needed.
Keeping a Number and Its Text Representation
A program has the integer value 32 and needs a text version of it.
Start with the value: The value 32 is an integer used as a number.
Convert the value: Calling str(32) produces the string representation "32".
Store the result: The new string can be assigned to a separate variable, while the original integer remains unchanged.
The program has both the original integer 32 and a separate string representation of it.
Predicting Common Conversions
What do you think happens?
What string representation does each call produce?
Reveal answer
Answer: str(32) produces "32", str(3.14159) produces "3.14159", str(True) produces "True", and str(None) produces "None".
Python examines the type of the supplied value and applies the representation rule for that type. Integers become their digits as text, floats retain their decimal point and digits, True becomes "True", and None becomes "None".
| Original value | Result of str() |
|---|---|
| 32 | "32" |
| 3.14159 | "3.14159" |
| True | "True" |
| None | "None" |
Common values and their string representations
The quotation marks in the right-hand column show that the result is text. They are not part of the displayed characters produced by the conversion. The important distinction is the type: 32 is an integer, while str(32) is a string containing the characters 3 and 2.
Combining Text and Values
score = 32 message = "Score: " + str(score)
Concatenation means combining text. When text is combined with a number or another non-string value, the non-string value must first be represented as text. str() supplies that representation, allowing the pieces to be processed as strings.
A program preparing a message, display value, or saved output may need to combine ordinary text with an integer, float, Boolean, None, or another value. Converting the value with str() standardizes it as text for that output task.
Mistakes with Conversion
Treating a number and its string representation as the same type
The first value is an integer used for numerical purposes, while the second is a string used for text.
Fix:
Track whether the program needs the original number or a text representation.Trying to combine text with a number before converting the number
The text and the number are different data types, so the number has not yet been prepared for text concatenation.
Fix:
Convert the number first, as in "Score: " + str(score).Assuming str() changes the original variable
str() returns a new string and does not modify the original value.
Fix:
Assign the returned string to a variable or use it directly.Expecting conversion to preserve numerical behavior
String conversion is intended for text-based processing or output, not for keeping the value in numeric form.
Fix:
Keep the original numeric value when later operations require a number.
Practice Before Output
Predict the result of each conversion, then explain which original values remain unchanged: str(8), str(2.5), str(False), and str(None).
Hints
- An integer becomes its digits as text.
- A float keeps its decimal point and digits in the representation.
- False and None become the corresponding text representations.
A program has count = 4 and needs the text Score: 4. Describe where str(count) belongs when the message is built by concatenating strings.
Hints
- The original count is an integer.
- The value must become a string before it is combined with other text.
Conversion Takeaways
- str() takes one value and returns a new string representing that value.
- Integers, floats, booleans, None, and other Python values can be converted with str().
- The original value is not modified by conversion.
- String conversion is useful when combining numbers or other values with text for messages, output, or storage.
- A value that looks similar after conversion can still have a different type and different behavior.
Key Takeaways
- str() converts a value into its string representation.
- The conversion returns a new string and leaves the original value unchanged.
- Common results include "32", "3.14159", "True", and "None".
- Use str() when a number or another value must be combined with text.
- Remember that a string representation is text, not the original numeric or other data type.