F.E. PPS Unit 3 Practical 2 solution

Practical 2: program to append a string to another string using the += operator.

Problem Statement

Write a Python program to append a string to another string using the += operator.
The program should:

  • Accept two strings as input from the user.
  • Append the second string to the first string using the += operator.
  • Display the updated string.

Solution Code

				
					# Program to append a string using += operator

# Input: Take two strings from the user
str1 = input("Enter the first string: ")
str2 = input("Enter the string to append: ")

# Appending using += operator
str1 += str2

# Output the updated string
print("Updated String:", str1)

				
			

Output

  • The program will take two string inputs from the user.
  • It will append the second string to the first using the += operator.
  • The result will be displayed.

Example output:

				
					Enter the first string: Hello
Enter the string to append: World
Updated String: HelloWorld
Process finished with exit code 0
				
			

Explanation

Below is the explanation of F.E. PPS Unit 3 Practical 2 solution where we have written program to append a string to another string using the += operator.

  1. Taking Input from the User:
    • The input() function is used to take two strings from the user and store them in str1 and str2.
  2. Appending Using += Operator:
    • The += operator is used to modify str1 by appending str2 to it.
    • This is equivalent to str1 = str1 + str2.
  3. Displaying the Result:
    • The updated string is printed using the print() function.

Key Concept Learned

  • Appending Strings Using += Operator:
    • The += operator allows modifying a string by adding another string to it.
  • Immutable Nature of Strings:
    • In Python, strings are immutable, but when += is used, a new string is created and assigned to the variable.
  • Handling User Input with Strings:
    • Demonstrates how to take and manipulate string input.
Tech Amplifier Final Logo