Turn Numbers into Text

Learn how to combine strings (text) and integers (numbers) in Python for clear and effective output. This tutorial covers the essential techniques and common pitfalls. …

Updated August 26, 2023



Learn how to combine strings (text) and integers (numbers) in Python for clear and effective output. This tutorial covers the essential techniques and common pitfalls.

Python is a powerful language for handling all sorts of data, including text (strings) and numbers (integers). Sometimes you’ll need to combine these different types of information, like displaying a message that includes both a greeting and someone’s age. This is where string concatenation comes in handy!

What is String Concatenation?

String concatenation simply means joining two or more strings together. Think of it like linking words together to form a sentence. In Python, we use the + operator to perform this joining action.

Example:

greeting = "Hello"
name = "Alice"

message = greeting + ", " + name + "!"
print(message) 

Output:

Hello, Alice!

In this example:

  1. We define two strings: greeting and name.
  2. We use the + operator to combine them with spaces and punctuation to create a complete message.
  3. Finally, we print the combined string.

Concatenating Strings and Integers: The Challenge

You can’t directly concatenate a string with an integer using the + operator. Python will throw a “TypeError” because it doesn’t understand how to combine different data types like this.

Example:

age = 25
message = "You are " + age + " years old."  # This will cause an error!

The Solution: String Conversion

To overcome this, we need to convert the integer (age) into a string before concatenating. We can do this using the str() function:

age = 25
message = "You are " + str(age) + " years old."
print(message) 

Output:

You are 25 years old.

Here, str(age) converts the integer 25 into the string "25" allowing us to concatenate it with other strings.

Common Mistakes and Tips:

  • Forgetting Conversion: The most common error is forgetting to convert integers to strings using str(). Always remember this step!
  • Spacing and Punctuation: Pay close attention to spacing and punctuation when concatenating. Add them explicitly within the strings you’re joining.

Example with Improved Spacing:

age = 25
message = "You are {} years old.".format(age)  # Using .format() for cleaner concatenation
print(message)

Output:

You are 25 years old.
  • f-strings (Formatted String Literals): A more modern and concise way to concatenate strings and variables is using f-strings:
age = 25
name = "Bob"
message = f"Hello, {name}! You are {age} years old."  # Notice the 'f' before the opening quote
print(message)

Output:

Hello, Bob! You are 25 years old.

This approach embeds variables directly within curly braces {}. It’s often considered more readable and less prone to errors than using + for concatenation.

Practice Makes Perfect

Experiment with different combinations of strings and integers to solidify your understanding of string concatenation. Try creating messages that include names, ages, scores, or any other relevant information!


Stay up to date on the latest in Computer Vision and AI

Intuit Mailchimp