How to concatenate two strings in Python

Joining two strings means combining them together to form one string in Python. This is possible in more than one way, depending on your needs. Here’s the explanation of different methods:

1. Using the + Operator

The + operator is a straightforward way to concatenate two or more strings. It directly combines the strings into one.

Code Example:

str1 = "Hello"
str2 = "Jobbinge"
result = str1 + " " + str2  # Adding a space between the strings
print(result)

Output:

Hello Jobbinge

Explanation:

  1. String Literals:
  • str1 is the literal string containing "Hello".
  • str2 is the literal string containing "Jobbinge".

2. Concatenation:

  • The + operator concatenates str1 with str2 placing a space " " between the strings.
  • Without the space, result would be the much less readable string "HelloJobbinge".

3. Output:

  • The final string "Hello Jobbinge" is stored in result and printed.

2. Using the join() Method

The join() method is a good option when concatenating multiple strings or strings stored in an iterable like a list.

Code Example:

str1 = "Hello"
str2 = "Jobbinge"
result = " ".join([str1, str2])  # Use a space as the separator
print(result)

Output:

Hello Jobbinge

Explanation:

  1. How join() Works:
  • "".join() separates using the space character ("").
  • It takes a list of strings ([str1, str2]) and joins them together using the separator.

2. Why Use join():

  • It is efficient and flexible, especially with a larger number of strings
  • If you wanted a different separator (like a comma), you could replace " " with ",".

3. Using Formatted Strings (f-strings)

F-strings, introduced in Python 3.6, provide a clean and readable way to concatenate strings and embed variables.

Code Example:

str1 = "Hello"
str2 = "Jobbinge"
result = f"{str1} {str2}"  # Embed variables directly into the string
print(result)

Output:

Hello Jobbinge

Explanation:

  1. How f-strings Work:
  • Prefix with an f before the string literal.
  • Use {} to place the variables into the string, namely str1 and str2.

2. Advantages:

  • They are readable and concise
  • Faster compared to the old methods, which include format() and %.

4. Using the % Operator

The % operator is an older method for string formatting and works by using placeholders.

Code Example:

str1 = "Hello"
str2 = "Jobbinge"
result = "%s %s" % (str1, str2)  # %s is a placeholder for strings
print(result)

Output:

Hello Jobbinge

Explanation:

  1. Placeholders:
  • %s is a placeholder for string values.
  • A tuple of values that replace the placeholders is (str1, str2).

2. Why Use This Method:

  • While less common in modern Python, it’s still supported and useful for older codebases.

5. Using the format() Method

The format() method allows you to dynamically insert values into a string using placeholders.

Code Example:

str1 = "Hello"
str2 = "Jobbinge"
result = "{} {}".format(str1, str2)  # Use {} as placeholders
print(result)

Output:

Hello Jobbinge

Explanation:

  1. How format() Works:
  • {} placeholders are replaced by values in the format() method.
  • The order of the values passed to format() determines where they are inserted.

2. Advantages:

  • It is more flexible than the % operator and supports extra formatting features.

6. Using Augmented Assignment (+=)

The += operator appends one string to another and updates the original string.

Code Example:

str1 = "Hello"
str2 = "Jobbinge"
str1 += " " + str2  # Append str2 to str1 with a space in between
print(str1)

Output:

Hello Jobbinge

Explanation:

  1. How It Works:
  • The += operator modifies str1 by appending " " + str2 to its original value.

2. Key Point:

  • This method directly updates str1, making it useful when iteratively building a string.

Key Takeaways:

  1. Strings are Immutable:
  • In Python, strings are immutable. They cannot be changed in-place. Concatenation always produces a new string.

2. Choosing the Right Method:

  • Use + for simple concatenation.
  • Use join() when working with a collection of strings.
  • Use f-strings for readability and speed in Python 3.6+.
  • Use format() or % for compatibility with older Python versions.