String Copy Program in Python

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!"

Copy one String to Another String in Python

Program 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

Program Explanation

This Python program creates a copy of a word you type in. When you run the program, it first asks you to enter a word and waits for you to type something. Once you enter your word, the program starts working on it one letter at a time. For each letter in your original word, it adds that letter to a new, empty word it’s making. It does this for every letter until it has copied all of them. When it’s done copying all the letters, the program shows you the new word it has made, which will look just like the one you typed in. This simple program is great for seeing how you can make a copy of a word letter by letter.

Program 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

Program Explanation

This Python program makes a copy of any text you type in. First, when you run the program, it asks you to “Please Enter a string:” which means it wants you to type some words. After you type your words and hit enter, the program uses a slice operation string[:] to take the whole text you entered, from the first character to the last, and creates a new variable called new_string that holds this copied text. It’s like using a photocopier to copy a note. Then, the program prints out “New String After Copy:” followed by the copied text. This shows you the text you entered, now stored in a new variable, proving that the program successfully copied your original text. This method is quick and reliable for duplicating text in Python.

Program 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

Program Explanation

This Python program copies any text you type in and shows you the copy. When you start the program, it first asks you to enter some text by showing the message “Please Enter a string:”. After you type your text and press enter, the program uses a command called str(string) to make sure the text is in the proper format to be used as a string, even though it’s already one. This doesn’t change your text but confirms it’s text. Then, it saves this text in a new variable called new_string. Finally, the program shows you the copied text by printing “New String After Copy:” followed by the text you entered. This helps you see that the program has made an exact copy of what you typed. This simple process shows how you can copy and display text in Python, ensuring it stays exactly the same.

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.