Online Tutorials & Training Materials | STechies.com
Register Login

4 Ways of Python String Concatenation with Examples

|| 0

4 Ways of Python String Concatenation with Examples
Stechies

When we join two or more string in one string with the help of operators or functions, this process known as string Concatenation.

In python we can join two strings by multiple ways.

For example we have two string here one is “Hello” and other one is “STechies” we will use different methods to join these two string into one string which is “Hello STechies”.

In Python, string concatenation can be done using different ways:

1.Using + Operator
2.Using Join() Method
3.Using { } Operators
4.Using f-string (Literal String Interpolation)

Using + Operator

The most common method to perform string concatenation is by using “+” operator, in this we need to just concatenate two string with + operator.

Example:

# Python program to explain String Concatenation Using "+" Operator

# Declare strings
str1 = 'Hello'
str2 = 'Stechies'
str3 = ' '

# Concatenation strings with "+" operator
str3 = str1+str3+str2

# Print output
print('Final String: ', str3)

Output:

Final String: Hello Stechies

Using join() method

Example:

# Python program to explain string concatenation using join method

# Declare strings
str1 = 'Hello'
str2 = 'Stechies'
str3 = ' '

# Join strings and print output
print('Final String: ', '' .join([str1, str3, str2]))

# Join String and get output in variable
final_string = ''.join([str1, str3, str2])

# Join strings and print output
print('Final String with variable: ', final_string)

Output:

Final String: Hello Stechies
Final String with variable: Hello Stechies

Using String Formatting with the { } Operators

Format function with curly braces are used commonly because when we use {} it reserve the placeholder for the variable and format() function pass the variable to the {} operator.

For integer value it is not necessary to convert integer in to string for concatenation.

Example:

# Python program to explain string concatenation using curly braces or {} operator 

# Declare strings
str1 = 'Hello'
str2 = 'Stechies'
str3 = ' '

# Join strings and print string using curly braces or {}
print("Final String: {}{}{}".format(str1, str3, str2))

Output:

Final String: Hello Stechies

Using f-string

This function is a bit advance form of function, it works with Python 3.6+ versions.

Example:

# Python program to explain string concatenation using f-string

# Declare strings
str1 = 'Hello'
str2 = 'Stechies'
str3 = ' '

# Join strings and print string using f-string
print(f'{str1} our site is {str2}')

# pass concatenation value in in varaible
final_string = f'{str1}{str3}{str2}'

# Print variable
print(final_string)

Output:

Hello Our site is Stechies
Hello Stechies

Concatenate String using Native Function

Example:

# Python program to explain String Concatenation using for loop by append string in loop

# Inilised a list
list1 = ['Our ', 'Site ', 'is ', 'stechies.com']

# Inilised string variable 
final_string = ''

# Append string in loop
for l1 in list1:
    final_string += l1
    
# print output
print(final_string)

Output:

Our Site is stechies.com

Concatenate String and Integer Variable

To concatenate string and integer variable, you need to convert integer variable to string and then concatenate using ‘+’ operator.

Example:

# Python program to explain String Concatenation Using "+" Operator

# Declare strings & Integer varaibles
str1 = 'Hello'
str2 = 'I am'
str3 = 25
str4 = 'years'
str5 = 'old'


# Concatenation strings with "+" operator
str3 = str1+' '+str2+' '+str3+' '+str4+' '+str5

# Print output
print('Final String: ', str3)

Output:

Traceback (most recent call last):
  File "main.py", line 12, in <module>
    str3 = str1+' '+str2+' '+str3+' '+str4+' '+str5
TypeError: Can't convert 'int' object to str implicitly


Correct Example:

# Python program to explain String Concatenation Using "+" Operator

# Declare strings & Integer variables
str1 = 'Hello'
str2 = 'I am'
str3 = 25
str4 = 'years'
str5 = 'old'

# Concatenation strings with "+" operator
str3 = str1+' '+str2+' '+str(str3)+' '+str4+' '+str5

# Print output
print('Final String: ', str3)

Output:

Final String: Hello I am 25 years old