All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

Python Concatenate String And Variable

Last Updated : Mar 11, 2024

Python Concatenate String And Variable

In this tutorial we will show you the solution of python concatenate string and variable, when you're learning a programming language, you're likely to come across a data type called a string.

A string usually contains a series of characters nested in quotation marks that can be represented as text.

String concatenation is the process of joining/adding one string to another. So, let’s get started.

Step By Step Guide On Python Concatenate String And Variable :-

String concatenation is important when working with two or more separate variables (strings) that are combined to form a much larger string.

To concatenate strings in Python, we use the “+” operator to append the strings to each other.

string1 = "Hello"
string2 = "World"
print("Hi" + string1 + string2)
temp = "Hi " + string1 + " " + string2 + "."
print(temp)
string = "Hi"
integer = 5
toString = str(integer)
print(string + " " + toString)
  1. In the above code, we will see two different examples of string concatenation with a variable.
  2. In the first example, we will perform concatenation between two string variables, and in the second example, we will perform concatenation between a string value and an integer value.
  3. In the first line, there is a variable named string1 that holds the value “Hello” in it.
  4. In the next line, there is another variable named string2 that contains a value of “World” in it.
  5. In the next line there is a print statement that performs the concatenation operation on both the variables “string1” and “string2” and a string saying “Hi”.
  6. The print statement will display “HiHelloWorld” in the output window, but it doesn’t look good for a user to see.
  7. So we can format the output by concatenating white spaces in the above string.
  8. In the next line, there is a variable named temp. This variable will store the resultant string generated from the concatenation operation.
  9. This concatenation operation is slightly different from the above concatenation.
  10. In this, we will include some white spaces in between to format the output in a way that pleases the eyes of the user.
  11. First there is the string saying “Hi ” that contains a white space at the end, then there is a “+” operator, and after that there is the variable “string1”, then again a “+” operator.
  12. After that, there is a single white space between the next “+” operator and the previous “+” operator. After the “+” operator, there is the second variable “string2”, and at the end, there is a full point (.) after the “+” operator.
  13. In the next line, there is a print statement that will display the value of the variable “temp”.
  14. In the next line, there is a variable called “string” that has the value “Hi” assigned to it.
  15. In the next line, there is another variable named “integer” which is of the int data type and has a value of “5” assigned to it.
  16. Now, to concatenate a string and an int, first you have to convert the int into a string data type. To do that, you can use the str() function that accepts one parameter, which is the value to be converted into a string. Here it will be the variable “integer” and the value will be stored in the variable “toString”.
  17. In the next line, the print statement will display the result of the concatenation performed between the variables string and toString.

Conclusion :-

So finally, in conclusion, we can say that with the help of this tutorial, you can now perform concatenation operations between a string value and a variable in a Python program.

You just have to put a “+” operator between two strings and it will get your work done.

I hope this tutorial on python concatenate string and variable helps you and the steps and method mentioned above are easy to follow and implement.