Concepts / String Concatenation and Combining Text

String Concatenation and Combining Text

String conversion changes how a value is represented for text-based work; it does not modify the original value.

  • Programming

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.

Key point

String conversion changes how a value is represented for text-based work; it does not modify the original value.

python

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

score32score32score_text"32"original value remainsstr(score)
What changes after str() is applied, and what remains unchanged in the original variable?

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.

Worked example

Keeping a Number and Its Text Representation

A program has the integer value 32 and needs a text version of it.

  1. 1
    Start with the value

    The value 32 is an integer used as a number.

  2. 2
    Convert the value

    Calling str(32) produces the string representation "32".

  3. 3
    Store the result

    The new string can be assigned to a separate variable, while the original integer remains unchanged.

Result

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?

  • str(32) produces "32", str(3.14159) produces "3.14159", str(True) produces "True", and str(None) produces "None".

  • Every call produces the same text: "value".

  • The calls keep their original data types instead of producing strings.

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 valueResult 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

32integerstr()string representation"32"string"Score: 32"combined textpass valuereturnscombine with text
How does a number become text before it can be combined with a string?
Code walkthrough
score = 32
message = "Score: " + str(score)
  1. 1
    The program begins with the integer value 32.
  2. 2
    str(score) returns the string "32" without changing score.
  3. 3
    The two strings are combined to produce "Score: 32".
  • score stores the original integer.
  • str(score) creates a string representation of that integer.
  • The plus operator can then combine the existing string with the converted string.
Output
"Score: 32"

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.

In the real world

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

  • Common mistake

    Treating a number and its string representation as the same type

    What it looks like

    32 and str(32) may look similar when displayed.

    Why it fails

    The first value is an integer used for numerical purposes, while the second is a string used for text.

    Do this instead

    Track whether the program needs the original number or a text representation.

  • Common mistake

    Trying to combine text with a number before converting the number

    What it looks like
    "Score: " + score
    Why it fails

    The text and the number are different data types, so the number has not yet been prepared for text concatenation.

    Do this instead
    Convert the number first, as in "Score: " + str(score).
  • Common mistake

    Assuming str() changes the original variable

    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

    Assign the returned string to a variable or use it directly.

  • Common mistake

    Expecting conversion to preserve numerical behavior

    What it looks like

    After str(32), the result is text rather than an integer.

    Why it fails

    String conversion is intended for text-based processing or output, not for keeping the value in numeric form.

    Do this instead

    Keep the original numeric value when later operations require a number.

Practice Before Output

EASY

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.
MEDIUM

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

  1. 1
    str() takes one value and returns a new string representing that value.
  2. 2
    Integers, floats, booleans, None, and other Python values can be converted with str().
  3. 3
    The original value is not modified by conversion.
  4. 4
    String conversion is useful when combining numbers or other values with text for messages, output, or storage.
  5. 5
    A value that looks similar after conversion can still have a different type and different behavior.

At a glance

  • 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.

Related concepts

More concepts starting with S