Learn adding two strings in python

INTRODUCTION

Hi everyone! In this article, we will be covering adding two strings in python in 3 easy steps. For those who are wondering what is python, It is a interpreter-based computer programming language.

FIRST TIME VISITED OUR WEBSITE???? If yes, then you are missing a lot! We are providing free cybersecurity & web development labs, content services and many more.

Cybersecurity Labs: Click Here

Web Development Labs: Click Here

WHAT IS STRING?

Source: Bartleby

First of all, Let us try to understand what is string. Basically, A String is a sequence of characters. For example “Faiyaz”, “BePractical”,”Testing” etc are all strings. Strings plays an important part in any software industry. Therefore, Getting an overall command on string will be very helpful for any programmer. We can also say that string is nothing but an array of characters. Let us see how we can add two strings in python

ADDING TWO STRINGS IN PYTHON: THE STEPS

Consider the code given below:

first_name = "Md"
middle_name = "Faiyaz"
last_name = "Ahmad"

Here, we have three variable first_name, middle_name and last_name with values “Md“, “Faiyaz” and “Ahmad” respectively. Now, let us try to see that how we can add all these strings to get a new string i.e “Md Faiyaz Ahmad”

first_name = "Md"
middle_name = "Faiyaz"
last_name = "Ahmad"

full_name = first_name + middle_name + last_name

Here, we have created a new variable full_name and used the + operator to add all these strings. Now, if we try to print full_name we will get “MdFaiyazAhmad”. But we need extra spaces in between. To do this, let us follow the same steps again.

#Final Code
first_name = "Md"
middle_name = "Faiyaz"
last_name = "Ahmad"

full_name = first_name + " " + middle_name + " " + last_name
print(full_name)

Now if we print this variable in to console, we will get the string “Md Faiyaz Ahmad”!

adding two strings in pytohn
Code
adding two strings in python- output
Output

This is how we can add two strings in python. Simple right? Well, In other languages like C or Java the complexity of writing the same program is very high. For example, This is how you can write the same program in C++

#include <iostream>
using namespace std;
int main(){
    string a = "Md";
    string b = "Faiyaz";
    string c = "Ahmad";
    string d = a + " " + b + " " + c;
    cout << d << endl;
    return 0;
}

And in Java, It will be something like this:

class HelloWorld {
    public static void main(String[] args) {
    String a = "Md";
    String b = "Faiyaz";
    String c = "Ahmad";
    String d = a + " " + b + " " + c;
    System.out.println(d);
    }
}

CONCLUSION

I hope you all have understood how we can add two strings in python. If there is any doubts or issue then please let me know in the comment section.

Join our telegram channel over here

Subscribe to our YouTube channel to learn more technical skills by clicking the link over here