Write a Python Program to Copy a String to another string with a practical example.
Python Program to Copy a String to another Example 1
Unlike other languages, you can assign one string to another with equal to operator. Or, you can Slice from starting to end and save it in another string. This Python program allows users to enter any string value. Next, we used the above-specified ways to copy the user-specified string.
# Python Program to Copy a String str1 = input("Please Enter Your Own String : ") str2 = str1 str3 = str1[:] print("The Final String : Str2 = ", str2) print("The Final String : Str3 = = ", str3)
Python Copy a String Example 2
In this program, we are using For Loop to iterate each character in a string and copy them to the new string.
# Python Program to Copy a String str1 = input("Please Enter Your Own String : ") str2 = '' for i in str1: str2 = str2 + i print("The Final String : Str2 = ", str2)
Python String Copy Example 3
This Python program is the same as the above example. However, we are using the For Loop with Range to copy a string.
# Python Program to Copy a String str1 = input("Please Enter Your Own String : ") str2 = '' for i in range(len(str1)): str2 = str2 + str1[i] print("The Final String : Str2 = ", str2)