How to Concatenate String and Int Objects in Python

Concatenation is the process of merging or combining strings. In this tutorial, let’s examine the different methods of concatenation that Python offers.

Concatenation using the ‘+’ operator

Python is known for its simplicity. And to combine two strings, let’s take a look at the following code:

a = “My”
b= “name”
c= “is”
d= “Van”
string = a+b+c+d
print(string)

When the program above is executed, it provides this output:

MynameisVan

All strings are each assigned to variables a, b, c, and d. Then, it works like the addition process and assigns the output to the variable named string. To display the concatenated strings, we use the function print(). Python is known for its simplicity. And to combine two strings, let’s take a look at the following code:

Now, how about we combine a string and an integer object? Here is an example:

a= print ‘Jordan’+23

It will result in this error:

Traceback (most recent call last):
File "C:\Users\Users1\sample.py", line 1
a= print ‘Jordan’+23
^
SyntaxError: invalid character in identifier

This is because you can’t use the ‘+’ operator in the same manner as the previous example. Hence, there are methods to follow in concatenating strings with integers.

String Formatting and Join Method

To concatenate strings and integers, let’s use the format() method. Here is a sample code:

firstName = 'Michael'
lastName = 'Jordan'
age = 57

print("{} {} is {} years old.".format(firstName,lastName,age))

The output:

Michael Jordan is 57 years old.

The format() method takes the suitable values and stores them as variables within the respective string. Also, don’t forget to add a period before the word format to execute the function properly.

.format

Also, you can use the join method in Python to concatenate a list of strings.
For example:

names = ['Liz', 'Anne', 'George', 'Belle']
print(', '.join(names))

The output:

Liz, Anne, George, Belle

Again, don’t forget to add a period before the word join in order to execute the function properly.

.join

How to convert String to Int in Python and vice versa

The int() method is used to convert a string to an integer value in Python. It takes two in two parameters:

  • the string to convert into an integer
  • the base you want your data to be represented in

The second parameter is optional.

Here’s the syntax for the int() method:

print(int("23"))

The output:

23

On the other hand, to convert an integer to a string, use the str() built-in method. This method takes an integer (or other data types) as an input and produces a string as an output.

Example:

average_score = 40
print(str(average_score)+" points")

The output:

40 points

How about failing to use the str() method?

average_score = 40
print((average_score)+" points")

The output:

Traceback (most recent call last):

File "C:\Users\Users1\sample1.py", line 3, in <module>
print((average_score)+" points")
TypeError: unsupported operand type(s) for +: 'int' and 'str'

It will output an error.

Conclusion

This tutorial demonstrates the simplicity of the Python programming language. Though it is a high-level language, it has user-friendly built-in methods to help you learn quickly. To learn more about common string operations, click here.

We will be happy to hear your thoughts

Leave a reply

Coders Eye - Web Dev Tutorials and How-To Guides for Beginners
Logo
Enable registration in settings - general