Concatenate String and Int, Variable, Or Date in Python

Hello Python learners, In this simple tutorial we will learn how to concatenate string and integer, staring and variable, or string and date.

In python programming, we generally use the plus operator (+) for concatenation.

Using the plus operator (+) you can concatenate two strings easily, but when you try to concatenate string and integer together you will get a traceback error. So, firstly we will learn to concatenate strings and integers and then Variable and Date.

Concatenate String and Integer, Variable or Date

Concatenate String and Int in Python

In order to concatenate string and Int in Python we have to use the format() function or str() function.

Well, what happens when we concatenate integers and strings using the plus operator (+) in python?

The answer is we get a traceback error.

Error while concatenating string and integer-

Age = 29
myNameAndAge = "My Name is Dips +Age"
print ( myNameAndAge )

Error in Output-

So, How to concatenate strings and integers?

As we already have mentioned that we will have to use the format() or str() function. So, let’s learn it with an example.

Concatenating two strings using the format() function

We use the format() function in Python to format the specified value and place them into a string placeholder.

Syntax-

Demo = "Demo String {}" .format(argument)

We have to use curly brackets {} as a placeholder in the string and then we have to pass the integer as an argument.

Example-

print ("My Name is Dips and my age is {} ".format(29))

Let’s have another example-

Age = 29
myNameAndAge = "My name is Dips and my age is {}" .format(Age)
print (myNameAndAge)

Output-

My name is Dips and my age is 29
Concatenate String and Integer in Python

Explanation-

In the above example, you can see we have concatenated strings and integer together using format() function. In the example code, we have created a variable which is containing an integer value and is concatenated with strings.

You can also do the same by following these rules-

  • Assign the integer value to a variable.
  • Place empty opening and closing brackets {} where you want to call the integer value.
  • And at the end of the string, use the .format(argument) function.

Note: You will have to pass the variable name as an argument in the format() function. Just like we have passed the age variable in the example code.

Concatenating String and Integer Using str() function

The str() function is basically used for converting other data types to strings. So, isn’t it possible to convert integer values to strings and concatenate them? Well, it is possible.

Let’s check with an example-

Name = "My Name is Dips"
Age = 29
print (Name +" And Age is "+ str(Age))

Output-

My Name is Dips And Age is 29

Explanation-

We have created a variable named “Name” and we have assigned string values to it. And then we created one more variable named “Age” and assigned an integer value.

In the next line, we called the print function and in the argument, we passed the “Name” variable and then concatenated some strings after that, we have used str() function to covert integer the value which is assigned to the “Age” variable.

One more example-

Age = 29
print ("My Name is Dips and my age is " + str(Age))

Output-

My Name is Dips and my age is 29

Concatenate String and Variable

It is one of the simplest things that you can do with strings. You can concatenate variables with strings using the format() function.

We already have learned how to use the format() function in Python. But just to recall we would like to explain once again.

We can use the format() function followed by the string and a dot operator. And pass the variable as an argument.

Follow the steps to concatenate String and Variable

  1. Create a variable and assign some value.
  2. Concatenate the variable with the string using the format() function.

Example-

Location = "Canada"
print ("I am from {} ".format(Location))

Output-

I am from Canada

Note: In order to concatenate a variable with the string using the format() function we use curly brackets as a placeholder inside the string.

Another Example-

Country = "USA"
print ("{} is the third largest country in the world in size" .format(Country))

Output-

USA is the third largest country in the world in size

It’s very easy to understand. Isn’t it?

Concatenate String and Date in Python

You can concatenate String and Date in Python programming easily by using the format() function which we have been using in this tutorial again and again.

Anyway, if you want to concatenate the date with strings then it is possible that you will be wishing to add the current date in the sentence.

So, In order to concatenate the current date in a string, we have to import date from the DateTime module so that we can get the current date every day or at least at the time you run the program.

You will have to follow the following steps-

  1. Import date from DateTime Module.
  2. Create a variable and assign some string values (Optional).
  3. Send the output.

Example-

from datetime import date
Today = "Today is {}".format(date.today())
print (Today)

Output-

Today is 2022-11-23

You can also skip creating a variable-

from datetime import date
print ("Today is {}".format(date.today()))

Conclusion

In this tutorial, we have explained how to concatenate string and int using the format() function and str() function. And also we covered string & variable concatenation and string & date concatenation techniques in Python programming.

We guided how to import the date from the DateTime module in order to concatenate the date with the string.

This tutorial is beginners friendly anyone can learn all the things that we have covered here. But still, If you have any queries or confusion then please comment down below, and we will try our best the help you.

Leave a Comment