Python Program to Copy one String to Another String

In this tutorial, we will learn to write a Python program to copy one String to another string. Strings are immutable objects in Python which means we cannot modify a string once created. So to perform the copy of the String, we need to create a new string and then perform a copy of the original string to the new string. In this post, we will see various approaches to copying a string to another string in Python.

For Example:

Input = "Hello World!"
Output = "Hello World!"

Method 1: Copy String using For Loop

string = input("Please Enter a string: ")
new_string = ""
for char in string:
    new_string += char
print("New String:", new_string)

Output

Please Enter a string: quescol
New String: quescol

Method 2: Copy String using Slice Operator

Slice operator can also be used to perform a copy of the original string to the new string. Below is the code to:

string = input("Please Enter a string: ")
new_string = string[:]
print("New String After Copy:", new_string)

Output

Please Enter a string: quescol
New String After Copy: quescol

Method 3: Copy String using Slice Operator

Python has a built-in function str(). We can use this function to create a copy of the original string. Below is the code to achieve this

string = input("Please Enter a string: ")
new_string = str(string)
print("New String After Copy:", new_string)

Output

Please Enter a string: quescol
New String After Copy: quescol

Conclusion:

In this post, we discussed different ways to copy a string to another string in Python. We can achieve this by using a loop, slice operator, or the built-in function str(). Hope this article is helpful for you.