Search⌘ K
AI Features

Python String Concatenation: Combine Text and Variables

Explore how to combine text and variables in Python by using string concatenation within the print function. Learn to mix strings, numbers, and variables to create dynamic and customized messages, enhancing your programming skills with practical examples.

Now that Python can remember things, let’s make our messages more dynamic!

This lesson will teach us how to combine words and numbers to create full sentences using variables.


Mix it up

Try this code:

Python
name = "Ava"
age = 25
print("Hi, my name is", name, "and I am", age, "years old.")

Awesome! We just combined strings and numbers in one line.


Why commas?

In print(), commas separate each part of our message. Python puts them together and even adds spaces for us.

We can mix:

  • Strings (text in quotes)

  • Variables (like name, age)

  • Numbers

All in a single print() statement.


Python
name = "Ava"
age = 25
print(f"Hi, my name is {name} and I am {age} years old.")

Your turn: Build a fun message

Python
food = "pizza"
color = "green"
animal = "dolphin"
print("My favorite food is ", food, "my favorite color is ", color, " and I want a pet ", animal, "!")

Now swap in your own favorite things!

We just made a custom sentence using commas.