Python String Concatenation

By | September 28, 2021
Python String Concatenation

Unlike other programming languages in Python, we do not represent the string as an array of characters, because Python does not support char data type. But to access an individual character from a string, we can use indexing with sq. [] brackets.

Example

str1 = "TechGeekBuzz"
print(str1[0])

Output

T

Python String Concatenation

String contamination is a method of combining two or more than two strings. And in Python, we have various ways to perform string concatenation.

Here are the major techniques we can use to perform string concatenation in Python.

Vamware
  • Use + operator.
  • Use string join() method
  • use % operator.
  • use format() function

+ Operator on String

To add two string, we can simply use the + or add operator. As its name suggests, it can add two string and return a copy of the combined string. Using the + operator, we can combine multiple strings together.

Example

Vamware
str1 = "Welcome"
str2 = "to"
str3 = "TechGeekBuzz"

combined = str1 + str2 +str3

print(combined)

Output

WelcometoTechGeekBuzz

<Note>: The + operator does not specify space between two strings when performing the concatenation. So we explicitly need to pass the space if we want spacing between the strings.

Example

str1 = "Welcome"
str2 = "to"
str3 = "TechGeekBuzz"

combined = str1 + " " + str2 + " " +str3

print(combined)

Output

Welcome to TechgeekBuzz

String .join() method

.join()is a string method which accepts sequential data structure (list, tuple set, and dictionary) as a parameter, and join all its elements together with a string operator and returns a string value. However, we can pass our own string variables to perform the concatenation operation. While performing the concatenation operation using join() method, we need to make sure that all the elements present in the sequential object are string, else the join() method throws an error.

.join() Syntax

"str seperator".join(list)

Example

str1= "Welcome to"
str2= "TechGeekbuzz"

combine1= " ".join([str1,str2])
combine2 = "-".join([str1,str2])

print(combine1)
print(combine2)

Output

Welcome to TechGeekbuzz
Welcome to-TechGeekbuzz

% Operator for string Formatting

This method of string formatting is borrowed from C language. Using the % operator and s (string) code specifier we set placeholder for string variables in a string represented by double inverted quotes. This string formatting techniques can also be used for string concatenation.

Example

str1= "Welcome to"
str2= "TechGeekbuzz"

combined = "%s %s" % (str1,str2)

print(combined)

Output

Welcome to TechGeekbuzz

Note: Here, the %s signify the string data type.

Python format() function

The format() function is generally used for string formatting or passing variable values in a string. But it can also be used for string concatenation.

Syntax

"{}".format()

Example

str1= "Welcome to"
str2= "TechGeekbuzz"

combined = "{} {}".format(str1,str2)

print(combined)

Output

Welcome to TechGeekbuzz

Python 3.6 and newer versions also support a new string formatting technique.

str1= "Welcome to"
str2= "TechGeekbuzz"

combined = f"{str1} {str2}"

print(combined)

Output

Welcome to TechGeekbuzz

Summary

  • Usually in Python string contamination is perform using + operator.
  • String formatting also provided an alternative approach for string concatenation.
  • The .join() method join all the elements of the list together with a string separator.

People are also reading: 

Leave a Reply

Your email address will not be published.