concatenate string in python



concatenate string in python :

Hello everyone, In this blog post we are see the concatenate string in python in detailed.
Before going to the blog post if you want to learn python for free then click on python tutorials point. so let's start:

In Python language, string are array of byte that representing Unicode character. However, Python does't  have a character data types, a single set  of characters is simply a strings with a lengths of 1. Square brackets []can be used to access element in the strings characters. python


concatenate string
fig 01) concatenate string in python 



Example:
# Python program to string operation

# Assign I Love You string to the variable char1 
char1 = "I Love You"

# Assign I Hate You string to the variable char2
char2= "I Hate You"

# print the result 
print(char1) 
print(char2) 
Output:
I Love You
I Hate You

Concatenation of string in python :

String Concatenation is the technique of combination of two string. There are many ways to String Concatenation.
For string concatenation we use following ways:
1. Using + operator
2. Using joint() method
3. Using % operator
4. Using Format function

1.Using + Operator

It is easy to use + operator for strings concatenation. This type of operator can be used to add multiple string together. However, the argument must be a strings.
Example: for + operator
# Python program to  + operator
# strings concatenation operation


# Defining of string
char1 = "LOVE "
char2 = "HATE"

# + Operator is used to combine DIFFERENT strings 
char3 = char1 + char2 
print(char3) 
Output:
LOVE HATE
Here, the char stores the string “LOVE ” and char2 stores the string “HATE”. The + Operator combines the stringS that is stored in the char1 and char2 and stores in another variable char3.


2.using join() Methods

The join() methods is a string method and return a string in which the element of sequence have been join by string separator.
Example:
# Python program to join method
# string concatenation operation 


var1 = "LOVE"
var2 = "HATE"

# join() methodS is used to combine the string 
print("".join([var1, var2])) 

# join() methodS is used here to combined
# the string with the separator Space(" ") 
var3 = " ".join([var1, var2]) 

print(var3) 
OUTPUT:
LOVEHATE
LOVE HATE
In the above examples, the variable var1 stores the string “LOVE” and variable var2 stores the string “HATE”. The join() methods combine the strings that is stored in the var1 and var2. The join methods accepts only the list as it is argument and list size can be anything. We can store the combined strings in another variable var3 which is separated by the space.

3. Using % Operator function

We can use % operator for string formatting purpose, it can also be used for string concatenation purposed. It is useful when we want to concatenate string and perform simple formatting opearation.
Example:
# Python program to demonstrate % operator for string
# string concatenation operation

var1 = "LOVE"
var2 = "HATE"

# % Operator is used here to combine the strings 
print("% s % s" % (var1, var2)) 

Output:
LOVE HATE
Here, the % Operators combine the strings that is stored in the var1 and var2. The %s denote strings data type. The value in both the variables is passed to the strings %s and becomes “LOVE HATE”.

4. Using format() function Oeration

str.formatis one of the string formatting method in Python language, which allow multiple substitution and value of formatting. This method let's us concatenate element within a strings through positional formatting.
Example:
# Python program to demonstrate str.format
# string concatenation operation


var1 = "LOVE"
var2 = "HATE"

# format function USE
# combine the strings 
print("{} {}".format(var1, var2)) 

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

print(var3) 
Output:
HATE LOVE
HATE LOVE
For similar kind of post click on pythonslearning and if you like this post then please comment and share.
BEST OF LUCK!!!
Tags: python concatenate list of strings-python concatenate strings and variable-concatenate string in python.
Previous
Next Post »

Featured Snippet

10 ways to iterate through a list in python

10 ways to iterate through a list in python The list is similar to array in other languages except for python, which provides the extra b...