python string concatenation 2021

python string concatenation

String Concatenation in Python
String Concatenation in Python

Python String Concatenation
In Python, Strings are arrays of bytes representing Unicode characters. However, Python does not have a character data type, a single character is simply a string with a length of 1.

Square brackets [] can be used to access elements of the string.


string concatenation in python

Concatenation


In Python, there are a few ways to concatenate – or combine – strings. The new string that is created is referred to

as a string object.

Obviously, this is because everything in Python is an object – which is why Python is an objected-oriented language.

One common task you’ll need to accomplish with any language involves merging or combining strings. This process is referred to as concatenation.

The best way to describe it is when you take two separate strings – stored by the interpreter – and merge them so that they become one.


This blog will describe how to concatenate strings in Python. There are different ways to do that, and we will discuss the most common methods. After, we will explore formatting, and how it works.


In order to merge two strings into a single object, you may use the “+” operator.

When writing code, that would look like this:


For instance, one string would be “hello” and the other would be “world.” When you use concatenation to combine them it becomes one string, or “hello world”.

Example :

# Assign Welcome string to the variable var1 

var1 = "Welcome"
  

# Assign statistics string to the variable var2 

var2 = "statistics"
 

# print the result 

print(var1) 
print(var2) 


Output:


Welcome
statistics

String Concatenation in Python


The String Concatenation is the technique of combining two strings. String Concatenation can be done by using many ways.


We can perform string concatenation using following ways:


1. Using + operator
2. Using join() method
3. Using % operator
4. Using format { } function


1. Using + operator :-

It’s very easy to use + operator for string concatenation. This operator can be used to add multiple strings together. However, the arguments must be a string.


Note:

Strings are immutable, therefore, whenever it is concatenated, it is assigned to a new variable.

example 1 :

a = "hello"
b = "world"
a + b


The final line in this code is the concatenation, and when the interpreter executes it a new string will be created.


output :


“hello world”

example 2 :

# taking two strings from the user 
a = 'Hello'
b = 'Python'
  
# using '+' operator concatenate them 
c = a+b 
  
# printing the result 
print("After Concatenation the string becomes", c)

Output :

HelloPython

One thing to note is that Python program cannot concatenate a string and integer.

  • These are considered two different types of objects. So, if you want to merge the two, you will need to convert the integer to a string.


The following example shows what happens when you try to merge a string and integer object.

>>> print ‘White’ + ‘Black’
WhiteBlack

>>> print ‘White’ * 3
WhiteWhiteWhite

>>> print ‘black’ + 3
Traceback (most recent call last):
File “”, line 1, in
TypeError: cannot concatenate ‘str’ and ‘int’ objects

For reference, the “>>>” characters indicate where the interpreter is requesting a command.


Notice how multiplying the string “White” three times returns the value “WhiteWhiteWhite”. This is important, so be sure to record it to memory.

In addition, we can see when trying to combine ‘black’ and 3 the interpreter spits out an error. This is where we are trying to concatenate a string and integer object, and it failed.


In layman’s terms,

  • A string can be any recorded characters but it’s most commonly

used to store words and information.

  • An integer on the other hand is a recorded number value that doesn’t have a decimal point. Python cannot add a word and number together.
  • It makes sense why the error happens when you look at it this way.


To make this possible, we can convert the number into a string using the appropriate function. The code for that would look like this:

print ‘black’ + str(3)
black3

The method we used to do this is the str() function.


Notice :-

how the interpreter simply combined the two objects and spit

them out when it was asked to print the data?

Operator Overloading in Python :

Operator Overloading in Python
Operator Overloading in Python

Refers to using the same operator to perform different tasks by passing different types of data arguments. To understand how ‘+’ operator works in different ways in python let us take the following example

# taking two numbers 
a = 3
b = 4
  
# using '+' operator add them 
c = a+b 
  
# printing the result 
print("The sum of these two numbers is ", c)

Output :

The sum of these numbers is 7

in above example we used ‘+’ operator to add numbers,

now let us take one more example to understand how ‘+’ operator is used to concatenate strings.

# taking two strings from the user 
a = 'abc'
b = 'def'
  
# using '+' operator concatenate them 
c = a+b 
  
# printing the result 
print("After Concatenation the string becomes", c)

Output :

After Concatenation the string becomes abcdef

String Concatenation Using + operator
Using + operator

2. Using join() Method


The join() method is a string method and returns a string in which the elements of sequence have been joined by str separator.

# Python program to demonstrate 
# string concatenation 
  
 
var1 = "Hello"
var2 = "World"
  
# join() method is used to combine the strings 

print("".join([var1, var2])) 
  
# join() method is used here to combine  
# the string with a separator Space(" ") 
var3 = " ".join([var1, var2]) 
  
print(var3) 

Output:

HelloWorld
Hello World

  • In the above example, the variable var1 stores the string “Hello” and variable var2 stores the string “World”.
  • The join() method combines the string that is storedin the var1 and var2. The join method accepts only the list as it’s argument and list size can be anything.
  • We can store the combined string in another variable var3
  • which is separated by space.


3. Using % Operator


We can use % operator for string formatting,

it can also be usedfor string concatenation. It’s useful when we want to concatenate strings and perform simple formatting.

# Python program to demonstrate 
# string concatenation 
  
 
var1 = "Hello"
var2 = "World"
  
# % Operator is used here to combine the string 

print("% s % s" % (var1, var2)) 

output :

Hello World

  • Here, the % Operator combine the string thatis stored in the var1 and var2.
  • The %s denotes string data type.
  • The value in both the variable is passedto the string %s and becomes “Hello World”.

4. Using format( ) function (python string concatenation)


While coding if you make use of the { }, it serves as placeholder for the variables that you would like to store inside a string. But in order to pass variables inside a string, you first need to use the format( ) function.

str.format() is one of the string formatting methods in Python, which allows multiple substitutions and value formatting. This method lets us concatenate elements within a string through positional formatting.

One of the main advantages of using the format( ) function is that, you do not need to convert an integer into a string before concatenating the data; it will automatically do the same for you. This is one of the main reasons as to why this method of concatenation is more preferred over others.

To understand this whole concept better, take a look at example below.

Fname = “John”
Lname = “Doe”
Age = “24”
print “{} {} is {} years old.“ format(fname, lname, age)

One more Example to understand this :

# Python program to demonstrate 
# string concatenation 
  
 
var1 = "Hello"
var2 = "World"
  
# format function is used here to  
# combine the string 

print("{} {}".format(var1, var2)) 
  
# store the result in another variable 
 
var3 = "{} {}".format(var1, var2) 
  
print(var3) 

output :

Hello World
Hello World

  • Here, the format() function combines the string that is stored in the var1 and var2 and stores in another variable var3.
  • The curly braces {} are used to set the position of strings.
  • The first variable stores in the first curly braces and second variable stores in the second curly braces.
  • Finally it prints the value “Hello World”.

To Read More :

String Manipulation in python

String Manipulation in python programs

Python String Concatenation in details

FAQ :-

Q1. How do you concatenate strings in Python 3?

ans. To concatenate strings, we use the + operator.

Keep in mind that when we work with numbers,

+ will be an operator for addition,

but when used with strings it is a joining operator.

Q 2. Why are strings immutable in Python?

ans. In Pythonstrings are made immutable so that programmers cannot alter the contents of the object (even by mistake).

This avoids unnecessary bugs. Some other immutable objects are integer, float, tuple, and bool. More on mutable and immutable objects in Python.

Q 3. How do you concatenate an array?

ans. The concat() method is used to join two or more arrays. This method does not change the existing arrays, but returns a new array, containing the values of the joined arrays.

Q 4. What is join () in Python?

ans.

Python String join() Method

The join() method takes all items in an iterable and joins them into one string. A string must be specified as the separator.

Q 5. How do you join in Python 3?

ans. The join() method is a string method and returns a string in which the elements of sequence have been joined by str separator. Syntax: string_name. 

join(iterable) string_name:

It is the name of string in which joined elements of iterable will be stored.