Skip to content

RESOURCES / BLOG

How to Concatenate Strings in Python

Hi everyone,

I’m learning Python while working on a project that involves generating dynamic image URLs using Cloudinary. Part of my task involves merging parts of strings, like image names, folder paths, or tags.

Can someone explain how to concatenate strings in Python in a clean and simple way? Bonus points if you can relate it to image filenames or web URLs!

Thanks in advance!

Great question, and very relevant if you’re working with image paths, filenames, or public URLs!

In Python, concatenating strings just means combining them together into one. There are multiple ways to do it, depending on your use case, and some methods are more efficient or readable than others.

Let’s explore the best techniques with examples tailored to tasks like building Cloudinary image URLs, folder paths, and tags.

The simplest way to concatenate (join) strings in Python is with the + operator.

folder = "images/"

filename = "sunset.jpg"

full_path = folder + filename

print(full_path)

# Output: images/sunset.jpgCode language: PHP (php)

This method works well when joining a small number of strings. Just make sure all values are actually strings; numbers won’t concatenate directly.

id = 42
text = "image" + id  # This causes an error!
# Fix: text = "image" + str(id)Code language: PHP (php)

Introduced in Python 3.6, f-strings are one of the most popular and readable ways to concatenate strings.

image_id = 2025

filename = f"cloudinary_image_{image_id}.jpg"

print(filename)

# Output: cloudinary_image_2025.jpgCode language: PHP (php)

You can insert variables directly into your string using {}.

cloud_name = "my-cloud"

public_id = "hero-banner"

url = f"https://res.cloudinary.com/{cloud_name}/image/upload/{public_id}.jpg"

print(url)Code language: PHP (php)

If you’re combining a list of strings, use the .join() method. It’s more efficient and cleaner than repeated.

tags = ["summer", "beach", "clouds"]

tag_string = ",".join(tags)

print(tag_string)

# Output: summer,beach,cloudsCode language: PHP (php)
upload_options = {

    "tags": ",".join(["portrait", "hdr", "profile"]),

}Code language: JavaScript (javascript)

The older .format() method still works and is useful when you can’t use f-strings (e.g., Python <3.6).

file = "{}_{}.{}".format("holiday", 2024, "jpg")

print(file)

# Output: holiday_2024.jpgCode language: PHP (php)

If you’re working with image metadata like resolution, timestamp, or user ID, you often need to merge strings with numbers.

width = 1080

height = 720

filename = f"image_{width}x{height}.jpg"

print(filename)

# Output:  image_1080x720.jpgCode language: PHP (php)

If you’re generating a batch of filenames:

for i in range(3):

    print(f"photo_{i}.jpg")

# Output: 

photo_0.jpg  

photo_1.jpg  

photo_2.jpgCode language: PHP (php)

Or collect them into a list:

image_list = [f"img_{i}.jpg" for i in range(1, 6)]

print(image_list)Code language: PHP (php)
cloud_name = "mycloud"

public_id = "product123"

transformation = "w_800,h_600,c_fill"

url = f"https://res.cloudinary.com/{cloud_name}/image/upload/{transformation}/{public_id}.jpg"

print(url)

# Output: https://res.cloudinary.com/mycloud/image/upload/w_800,h_600,c_fill/product123.jpgCode language: PHP (php)

String concatenation is at the heart of building dynamic URLs like this.

TaskBest Method
Combine a few strings+or f-strings
Build dynamic filenamesf-strings
Join list of tags",".join(tags)
Add numbers to stringsf-strings or str()
Older Python compatibility.format()
  • Mixing strings with integers without casting .str() is your friend.
  • Using + in loops with lots of strings. Use .join() instead.
  • Forgetting to include separators like slashes or commas
MethodExample CodeBest For
+"image_" + str(id) + ".jpg"Quick joins
f-stringf"image_{id}.jpg"Readable dynamic strings
.join()",".join(["tag1", "tag2"])Joining lists
.format()"{}-{}.jpg".format(name, size)Compatibility with older Python

Whether you’re uploading images to Cloudinary, naming files, generating URLs, or formatting data, string concatenation is a fundamental skill in Python. For modern, readable code, we recommend f-strings. They’re fast, clear, and perfect for working with filenames, tags, or even full asset URLs.

Start Using Cloudinary

Sign up for our free plan and start creating stunning visual experiences in minutes.

Sign Up for Free