How To Concatenate A String And An Integer In Python

Concatenate a String and an Integer in Python

There are many ways to concatenate a string and an integer in Python. In this article, I will give you the most straightforward and easiest-to-understand solutions to handle it.

Concatenate a string and an integer in Python

Using str() method

str() is a method used to convert other data types in Python to string types, the result of this method is applied in many different problems, including to concatenate a string and an integer.

Syntax:

str(object, encoding='utf-8', errors='strict')

Parameters:

  • object: An object that can be displayed as a string. If not provided, an empty string is returned.
  • encoding: the desired encoding type of an object. If not provided, the default encoding is UTF-8.
  • errors: Response when encoding error. The default value is ‘strict’.

I will check the data type of each variable after using the str() method as follows:

intValue = 3
result = str(intValue)

print('Data type of intValue is: ') 
print(type(intValue))
print('Data type of result is: ') 
print(type(result))

Output:

Data type of intValue is: 
<class 'int'>
Data type of resultis:
<class 'str'>

After using the str() method, the data type changed to string. However, its original data type still exists in the intValue variable. When I need to use it, I can still get it.

We can use result to concatenate a string like this:

intValue = 3
result = str(intValue)
print('This is a String: ' + result)

Output:

This is a String: 3

In this case, if you don’t convert intValue to string data type, using addition will cause the error because it is not the same data type.

intValue = 3
result = str(intValue)
print('This is a String: ' + intValue) 

Output:

Exception has occurred: TypeError
can only concatenate str (not "int") to str
  File "D:\workspace\python\hello.py", line 17, in <module>
    print('This is a String: ' + intValue)

Using f-string

Python f-string was introduced in version 3.6 of Python. Using this syntax means that any expressions you use in this syntax during command execution will be in string form.

Syntax:

f '{value1} {value2}'

Parameters:

  • value1, value2: The values you want to concatenate.

This syntax is quite effective if you have too many variables to concatenate. Converting each variable to use is very time-consuming. Follow the example below to understand better:

myAge = 20
print(f'I am {myAge} years old!!')

Output:

I am 20 years old!!

Using this syntax also doesn’t affect the data type you need to concatenate. I can check the following:

myAge = 20
print(f'I am {myAge} years old!!')
print(type(myAge))

Output:

I am 20 years old!!
<class 'int'>

As you have seen, the data type of myAge is still int.

Summary

No matter which method is used to concatenate a string and an integer in Python, I still believe each method has its advantages. If you need string data for other calculations, I recommend you use the first method. Otherwise, if you don’t need it, use the 2nd method. Good luck with your studies.

Maybe you are interested:

Leave a Reply

Your email address will not be published. Required fields are marked *