Python Program to Copy a String

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 string copy output

Please Enter Your Own String : Hi Guys
The Final String : Str2  =  Hi Guys
The Final String : Str3  = =  Hi Guys

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 Program to Copy a String 2

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)

Python string copy output

Please Enter Your Own String : Python Programs With Examples
The Final String : Str2  =  Python Programs With Examples
>>> 
Please Enter Your Own String : Hello World
The Final String : Str2  =  Hello World