How to Concatenate String and Integer in Python
To concatenate a string and an integer in Python, you can use the str() function along with the + operator.
The following example shows how to combine a string and an integer in Python.
Using str() Function and + Operator
We can use the str() function to convert an integer to a string and then use the + operator to concatenate them.
Suppose we have the following integer:
# Declare int
age = 45
# Combine int and string
message = "I am " + str(age) + " years old"
# Show result
print(message)
Output: 👇️
I am 45 years old
In this example, we use the str() function to convert the integer age to a string and then use the + operator to concatenate it with other strings to form the message.
Conclusion
We can use the str() function along with the + operator to concatenate a string and an integer in Python. This method provides a convenient way to combine different data types into a single string.