All TalkersCode Topics

Follow TalkersCode On Social Media

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

How To Concatenate String And Int In Python

Last Updated : Mar 11, 2024

How To Concatenate String And Int In Python

In this tutorial we will show you the solution of how to concatenate string and int in python, Python supports string concatenation using the + operator.

In most programming languages, if we concatenate a string with an integer or any other primitive data type, the language takes care of converting them to string and then concatenating them.

Python doesn’t support this feature by default. Python throws a runtime error when performing such an operation. So, let’s get started.

Step By Step Guide On How To Concatenate String And Int In Python :-

Normally, string concatenation is done in Python using the + operator. However, when working with integers, the + represents addition.

Because of this, Python will raise an error, a “TypeError” to be exact, when the program is run.

Python allows the program to compile, but Python doesn’t allow you to run the program.

Python provides some built-in methods that can be useful for performing this task.

  • Using str() function
  • Using % operator
  • Using the format() function
  • Using f-strings
string = "Hello, my age is "
integer = input("Enter you age : ")
int(integer)
print(string + str(integer))
print("{}{}".format(string, integer))
print("%s%s" % (string, integer))
print(f"{string}{integer}")
# print(string + integer)
  1. In the first line, there is a variable named string that has the value “Hello, my age is ” assigned to it.
  2. In the next line, there is a variable named integer that will store the value given by the user using the input() function.
  3. The value returned from the input() function will be of the string data type, but the value we want is of the integer data type.
  4. To convert any string value into an integer, you can use the int() function. It will accept one argument, that is the value to be converted into an integer data type.
  5. In the next line, the value taken from the user will be converted into an integer data type using the int() function.
  6. In the next line, there is a print statement that performs string concatenation with an integer value with the use of the str() function.
  7. The str() function accepts one argument which is the value to be converted into a string data type. In this case, it will convert the value of the variable “integer” to an integer. The print statement will print the result as a string value.
  8. In the next line, there is another print statement that will perform the concatenation by using the format() function.
  9. The format() uses curly braces to insert variables into strings. We don’t need to worry about first converting our integer into a string to concatenate it. We can simply pass in the value or the variable that holds the integer.
  10. In the next approach, we can use the ‘%’ format specifier to concatenate a string value and an integer value. You just have to specify both the value as a string by using the “%s” format specifier.
  11. In the last approach, you can use the f-strings to perform the operation in your program.
  12. The f-strings in Python are created by prepending the letter ‘f’ or ‘F’ in front of the opening quotes of a string.
  13. All the four print statements will display the same value as the output in the output window.
  14. In the last line, there is a comment that has a print statement that directly concatenates both the values. But this line is a comment because it will raise a runtime error.

Conclusion :-

So finally, in conclusion, we can say that with the help of this article, you can now perform concatenation operation on a string value and an integer value in a Python program.

The best and simplest way is to use the str() function but you can apply any of the methods mentioned above.

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