Python: Program to Concatenate Two Strings
Python: Program to Concatenate Two Strings

Python: Program to Concatenate Two Strings

String concatenation is the process of joining two or more strings end-to-end. In Python, there are multiple ways to concatenate strings. Here, we’ll explore two common methods: using the + operator and The join() method.

Method 1: Using the + Operator

The + operator is a straightforward way to concatenate two strings.

Python
def concatenate_strings_plus(str1, str2):
    return str1 + str2

# Example usage
str1 = "Hello"
str2 = "World"
print("Concatenated string using +:", concatenate_strings_plus(str1, str2))

Explanation:

Function Definition (concatenate_strings_plus):

  • concatenate_strings_plus(str1, str2): This function takes two parameters str1 and str2, which are the input strings to be concatenated.

Using the + Operator:

  • return str1 + str2: Concatenates str1 and str2 using the + operator and returns the resulting string.

Example Usage:

  • str1 = “Hello” and str2 = “World”: Initializes two string variables.
  • print(“Concatenated string using + :”, concatenate_strings_plus(str1, str2)): Prints the concatenated result of str1 and str2 using the + operator.

Method 2: Using join()

The join() method can also be used to concatenate strings. It is particularly useful when you need to concatenate a list of strings.

Python
def concatenate_strings_join(str1, str2):
    return ''.join([str1, str2])

# Example usage
print("Concatenated string using join():", concatenate_strings_join(str1, str2))

Explanation:

Function Definition (concatenate_strings_join):

  • concatenate_strings_join(str1, str2): This function takes two parameters str1 and str2, which are the input strings to be concatenated.

Using join():

  • return ”.join([str1, str2]): Uses the join() method to concatenate str1 and str2. The join() method concatenates all elements of the list [‘str1’, ‘str2’] into a single string.
  • is used as the separator, indicating that there should be no additional characters between str1 and str2.

Example Usage:

  • print(“Concatenated string using join():”, concatenate_strings_join(str1, str2)): Prints the concatenated result of str1 and str2 using the join() method.

Summary:

Purpose: To demonstrate different methods for concatenating two strings in Python.

Input: Two strings (e.g., “Hello” and “World”).

Output: A single concatenated string (e.g., “HelloWorld”).

Methods:

  • Using + Operator: Simple and direct approach to concatenate two strings.
  • Using join(): Useful for concatenating a list of strings with a specified separator.

Example Output:

For the input strings “Hello” and “World”, the output will be:

Python
Concatenated string using +: HelloWorld
Concatenated string using join(): HelloWorld

These programs illustrate two common and effective methods to concatenate strings in Python, showcasing the versatility and simplicity of string operations in the language.


Discover more from lounge coder

Subscribe to get the latest posts sent to your email.

Leave a Reply

Your email address will not be published. Required fields are marked *

Discover more from lounge coder

Subscribe now to keep reading and get access to the full archive.

Continue reading