Python Program to Swap Two Strings

« Previous Program Next Program »

Swap Two Strings in Python

To swap two strings in python, first ask from user to enter value of both the string. After entering value of the two strings, just swap the two string using the third variable. After swapping the two strings, print the result as output.

Python Programming Code to Swap Two Strings

Following python program ask from user to enter the value of two strings to swap both the string, and then print the result as output:

# Python Program - Swap Two Strings
while True:
	print("Enter 'x' for exit.")
	string1 = input("Enter First String: ")
	string2 = input("Enter Second String: ")
	if string1 == 'x':
		temp = string1
		string1 = string2
		string2 = temp
		print("\nString after swapping:")
		print("First String:", string1)
		print("Second String:", string2,"\n")

Here is the sample run of the above python program to illustrate how to swap two strings in python:

python swap two strings

Same Program in Other Languages

You may also like to learn or practice the same program in other popular programming languages:

« Previous Program Next Program »


Quick Links
Signup - Login - Give Online Test