×

How to Concatenate Two Strings in Python

How to Concatenate Two Strings in Python

Like the other data types, operations on strings are quite useful when we deal with some real-life applications.

Here we will talk about a simple operation on strings which is concatenation.

Before that, let's have a glance at what strings are?

Strings are a sequence of characters that are denoted using inverted commas ''. They are immutable which means they cannot be changed once declared. The distinction between two strings can be understood using id().

Examples of a string are-

a=’Bill Gates’

x=’Moscow’

In this article, we will discuss how we can concatenate two strings with the help of the given methods-

  1. Using ‘+’ operator.
  2. Using .format()
  3. Using join()
  4. Using % operator.

So, let’s get started…

  1. The first way which we will see is concatenating two strings using the '+' operator. This operator merges or joins the two given strings.

In the program given below, we have declared six strings and then concatenated them using the '+' operator and stored them in str7 and after that print that value.

The following program illustrates the same-

 #declaring the strings
 str1="Python"
 str2=" is a"
 str3=" very"
 str4=" interesting"
 str5=" programming"
 str6=" language."
 #concatenating the strings using '+' operator
 str7=str1+str2+str3+str4+str5+str6
 print("str7 contains the concatenated string - {}".format(str7)) 

INPUT-

Concatenate Two Strings

OUTPUT-

Concatenate Two Strings

In the output, we can observe that all the six strings that we declared are now converted into a single sentence using the '+' operator.

The second method that we will discuss is concatenating the strings using format().

So whenever we are using format() in the print function, we need to specify the curly braces or the placeholders at the required positions, since they are the ones that will take up the values of variables we specify in the parentheses of format.

In the program given below, we are printing a sentence using the string values.

The following program illustrates the same-

 #declaring the strings
 str1="Python"
 str2=" is a"
 str3=" very"
 str4=" interesting"
 str5=" programming"
 str6=" language."
 #concatenating the strings using format()
 print("str7 contains the concatenated string - {}{}{}{}{}{}".format(str1,str2,str3,str4,str5,str6)) 

INPUT-

Concatenate Two Strings

OUTPUT-

Concatenate Two Strings

In the output we can observe that the values of str1, str2, str3, str4, str5, and str6 are there in the placeholders we provided in the print statement.

Therefore, all the strings are now converted into a single sentence using format().

The third method we will discuss is concatenating our strings using join().

The join() method takes an iterable that contains a value and a separator and then it joins our strings.

The following program illustrates the same-

 #declaring the strings
 str1="Python"
 str2=" is a"
 str3=" very"
 str4=" interesting"
 str5=" programming"
 str6=" language."
 #concatenating the strings using format()
 str7="".join([str1,str2,str3,str4,str5,str6])
 print(str7) 

INPUT-

Concatenate Two Strings

OUTPUT-

Concatenate Two Strings

We have not specified anything in the separator and provided all the six strings as the parameters for join().

When we print str7, we can observe that all the strings are joined, and we can see a meaningful sentence on our screen.

In the last method, we will use the "%" operator.

The % operator is used for the formatting of strings, in the program given below %s denotes a string and then they are replaced by the values of respective variables.

The following program illustrates the same-

 #declaring the strings
 str1="Tutorials"
 str2=" &"
 str3="Examples"
 #concatenating the strings using format()
 print("%s %s %s"%(str1,str2,str3)) 

INPUT-

Concatenate Two Strings

OUTPUT-

Concatenate Two Strings

So we have declared three strings in this program and then used %s in the print function. We can observe in the output that all the three %s are replaced by the values of str1,str2, and str3.

In the next program, we did the same thing with six strings.

Let’s see what happens when we execute the given program-

 #declaring the strings
 str1="Python"
 str2=" is a"
 str3=" very"
 str4=" interesting"
 str5=" programming"
 str6=" language."
 #concatenating the strings using format()
 print("%s %s %s %s %s %s"%(str1,str2,str3,str4,str5,str6)) 

INPUT-

Concatenate Two Strings

OUTPUT-

Concatenate Two Strings

So, we have declared six strings in this program and then used %s in the print function. We can observe in the output that all the six %s are replaced by the values of str1,str2, str3,str4, str5, and str6.

And now the strings are converted to a meaningful sentence.

So, in this article, we discussed what are the different ways of concatenating two strings in Python.


Related Topics

Python Pass Statement

The pass statement is a null statement. The difference between pass and comment is that comment is ignored by the interpreter, whereas pass is not. The pass statement is typically used...

3 minutes read.

How to Declare a Variable in Python?

The concept of constants and variables is something that we are studying right from our primary classes. We know that constants are the fixed values whereas variables are those whose...

4 minutes read.

How to check data type in python

The data type represents the nature of a variable; it determines what kind of data it can store and what operations can be carried out on it. There are five...

4 minutes read.

Python String endswith() method

Python String endswith() method The string.endswith() method in Python returns a Boolean value True if the string ends with the specified suffix, otherwise it returns False. Syntax endswith(suffix[, start[, end]]) Parameter suffix – This parameter represents a string or tuple...

2 minutes read.

Cross Entropy in Python

Introduction Cross-entropy loss is frequently combined with the softmax function. Determine the total entropy among the distributions or the cross-entropy, which is the difference between two probability distributions. For the purpose...

5 minutes read.

Python program to display ASCII value of the character

Python program to display ASCII value of the character The full form of ASCII is American Standard Code for Information Interchange. This example explains a program in Python to find the...

1 minute read.

Python SciPy Library

Introduction SciPy, a logical library for Python is an open-source, BSD-authorized library for arithmetic, science, and design. The SciPy library relies upon NumPy, which gives advantageous and quick N-dimensional exhibit control....

6 minutes read.

Python Escape Characters

In this tutorial, we will learn how to use the Escape Characters in Python. Escape Character: Escape Characters are used for some special meaning in our statements. It is denoted or represented...

3 minutes read.

PyPi TensorFlow

It is a free open-source library for high-performative numerical computations. The architecture of TensorFlow allows us for easy deployment and computing across a varied number of platforms and from desktops...

3 minutes read.

Find Median of List in Python

The median is an enlightening measurement that is utilized as a proportion of the focal inclination of a circulation. It is equivalent to the centre worth of the conveyance. There...

3 minutes read.

How to set up a proxy using selenium in python

What is Proxy? A proxy acts as a middleman between user requests and server responses. The main purposes of proxies are to protect user privacy and encapsulate data between various interactive...

3 minutes read.

Python String join() method

Python String join() method The String.join() method in Python concatenates each element of an iterable (such as list, string and tuple) to the given string and returns the concatenated string. Syntax string.join(seq) Parameter Seq: This...

1 minute read.

Python Dictionary Methods

Python Dictionary Methods Python has a set of built-in methods that dictionary objects can call. All the python dictionary methods are as follow: Methods Description clear() The dictionary.clear() method removes all the elements...

3 minutes read.

Sublime Python

SUBLIME: A compact, cross-platform code editor called Sublime Text 3 (ST3) is well-known for its quickness, usability, and robust community support. Although it's a fantastic editor out of the box, its...

6 minutes read.

Number pattern in Python

Number pattern in Python: This article explains how to print number patterns in Python. The FOR loop, while loop, and range() functions are used in the following Python programs to...

4 minutes read.

Python String isalnum() method

Python String isalnum() method The string.isalnum () method in Python returns a boolean value true if all characters in the string are alphanumeric else for any other value it returns false. Syntax String.isalnum() Parameter NA Return This...

1 minute read.

Python Support

Python Python is an interactive and more accessible language than any other programming language. The python programming language uses a variety of libraries to perform the operations in a faster way....

3 minutes read.

Type casting in Python

Introduction Type Casting is defined as the method of converting the variables/values data type into a certain data type for matching the operation needed to be performed by the users. This...

4 minutes read.

Python Set union() method

Python Set union() method The set.union() method in Python returns a set that contains all items from the original set and all items from the specified sets. Syntax set.union(set1, set2...) Parameter set1- This parameter represents the first set...

2 minutes read.

Crash Course on Python by Google

There is a new, free Python programming course offered by Google on Coursera. No programming experience is necessary. There are numerous Python courses available, but when you learn that Google is...

5 minutes read.