Practical 1: Program to Concatenate Two Strings Using + Operator
Problem Statement
Write a Python program to concatenate two strings using the + operator.
The program should:
- Accept two strings as input from the user.
- Concatenate the two strings using the + operator.
- Display the concatenated result.
Solution Code
# Program to concatenate two strings using + operator
# Input: Take two strings from the user
str1 = input("Enter the first string: ")
str2 = input("Enter the second string: ")
# Concatenation using + operator
result = str1 + str2
# Output the concatenated string
print("Concatenated String:", result)
Output
- The program will take two string inputs from the user.
- It will concatenate the strings using the + operator.
- The result will be displayed.
Example output:
Enter the second string: There
Concatenated String: HiThere
Process finished with exit code 0
Explanation
Below is the explanation of F.E. PPS Unit 3 Practical 1 solution where we have written Python program to concatenate two strings using the + operator.
- Taking Input from the User:
- The input() function is used to take two strings from the user and store them in str1 and str2.
- Concatenation Using + Operator:
- The + operator is used to join str1 and str2, forming a single string.
- The result is stored in the variable result.
- Displaying the Result:
- The concatenated string is printed using the print() function.
Key Concept Learned
- String Concatenation:
- The + operator can be used to join two or more strings in Python.
- Handling User Input with Strings:
- Demonstrates how to take string input from the user and process it.
- Basic String Operations in Python:
- Understanding fundamental string operations like concatenation.