Tutorials  /  Python

Concatenate String and Int in Python

Ccentron Redaktion · December 2024 ·3 min read ·Python, Tutorial

Introduction

Python supports string concatenation using the + operator. In most other programming languages, if we concatenate a string with an integer (or any other primitive data types), the programming language automatically takes care of converting them to a string and then concatenates it seamlessly.

However, in Python, if you try to concatenate a string with an integer using the + operator, you will get a runtime error.

VM

Matching infrastructure at centron

Scripts and services eventually need an environment that keeps running: ccloud³ VMs from €3.12 per month, billed by the hour. Rent a cloud server →

Example for Concatenate String and Int

Let’s look at an example for concatenating a string (str) and an integer (int) using the + operator.

Python

string_concat_int.py
current_year_message = 'Year is '

current_year = 2018

print(current_year_message + current_year)
  

The desired output is the string: Year is 2018. However, when we run this code we get the following runtime error:

Python

Traceback (most recent call last):
  File "/Users/sammy/Documents/github/journaldev/Python-3/basic_examples/strings/string_concat_int.py", line 5, in <module>
    print(current_year_message + current_year)
TypeError: can only concatenate str (not "int") to str
  

So how do you concatenate str and int in Python? There are various other ways to perform this operation.

Prerequisites for Concatenate String and Int

In order to complete this tutorial, you will need:

  • Familiarity with installing Python 3 and coding in Python. Check out the How to Code in Python 3 series or use VS Code for Python.
  • This tutorial was tested with Python 3.9.6.

Using the str() Function

We can pass an int to the str() function, and it will be converted to a str:

Python

print(current_year_message + str(current_year))
  

The current_year integer is returned as a string: Year is 2018.

Using the % Interpolation Operator

We can pass values to a conversion specification with printf-style String Formatting:

Python

print("%s%s" % (current_year_message, current_year))
  

The current_year integer is interpolated to a string: Year is 2018.

Using the str.format() Function

We can also use the str.format() function for concatenation of string and integer:

Python

print("{}{}".format(current_year_message, current_year))
  

The current_year integer is type coerced to a string: Year is 2018.

Using f-strings to Concatenate String and Int

If you are using Python 3.6 or higher versions, you can use f-strings, too:

Python

print(f'{current_year_message}{current_year}')
  

The current_year integer is interpolated to a string: Year is 2018.

Conclusion

You can check out the complete Python script and explore many additional Python examples, tutorials, and detailed guides directly from our GitHub repository for free.

Jetzt 200 € Guthaben sichern

Testen Sie Ihr Setup auf ccloud³

Registrieren Sie sich in der ccloud³ und erhalten Sie 200 € Startguthaben für Ihr Projekt – z. B. für eine PostgreSQL-VM mit automatischen Backups.

centron Redaktion Technische Redaktion

Das Redaktionsteam von centron schreibt Anleitungen aus dem Betriebsalltag: getestet auf unserer eigenen Plattform, betrieben im Rechenzentrum in Hallstadt bei Bamberg.

Kategorie Python
Teilen
Noch offene Fragen?

Our team will help you with your specific setup - in German or English, by people who run the platform themselves.

War dieses Tutorial hilfreich?

Your answer is stored anonymously and helps us improve our tutorials.

Kommentare

No comments yet - be the first to ask a question about this tutorial.

Sign in to comment

Comments are open to centron customers. Sign in to your account to ask a question about this tutorial.

Weiterlesen

Das könnte Sie auch interessieren

Jetzt kostenlos anfangen

Melden Sie sich an und erhalten Sie in den ersten 60 Tagen ein Guthaben von 200 € bei centron.

Dieses Werbeangebot gilt nur für neue Konten. Angebot ausschließlich für Gewerbetreibende.

Jetzt loslegen Sales kontaktieren