Python String Concatenation: Combine Text and Variables

Learn how to combine variables and text in print messages.

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.

A cleaner way: f-strings

Putting an f before the opening quote turns the string into an f-string. Inside an f-string, you can drop variables straight into the text by wrapping them in curly braces { }, so you don't need to break the sentence up with commas. The result is the same, but the code reads more like the sentence it produces.


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.

How do you reverse a string in Python?

In Python, strings are ordered sequences of character data.There is no built-in method to reverse a string. However, strings can be reversed in several different ways.

Methods

Three methods to reverse a string are explained below:

1. Slicing

Strings can be reversed using slicing. To reverse a string, we simply create a slice that starts with the length of the string, and ends at index 0.

To reverse a string using slicing, write:

stringname[stringlength::-1] # method 1

Or write without specifying the length of the string:

stringname[::-1] # method2

The slice statement means start at string length, end at position 0, move with the step -1 (or one step backward).

Python 3.14.0
s="Python" # initial string
stringlength=len(s) # calculate length of the string
slicedString=s[stringlength::-1] # slicing
print (slicedString) # print the reversed string

2. Loop

To start, let’s create a new array called reversedString[].

We can then loop over the string with iterating variable index initialized with the length of the string.

  • In each iteration, concatenate value of str[index-1] with reverseString

  • Decrement the index.

We then simply keep iterating until the index is less than zero.

Python 3.14.0
s = "Python" # initial string
reversedString=[]
index = len(s) # calculate length of string and save in index
while index > 0:
reversedString += s[ index - 1 ] # save the value of str[index-1] in reverseString
index = index - 1 # decrement index
print(reversedString) # reversed string

3. Use join

This is a powerful technique that takes advantage of Python’s iterator protocol. This technique reverses a string using reverse iteration with the reversed() built-in function to cycle through the elements in the string in reverse order and then use .join() method to merge all of the characters resulting from the reversed iteration into a new string.

The general syntax is

s="Python"
reversedstring=''.join(reversed(s))

The following Python code demonstrates the concept.

Python 3.14.0
s = 'Python' #initial string
reversed=''.join(reversed(s)) # .join() method merges all of the characters resulting from the reversed iteration into a new string
print(reversed) #print the reversed string