VOOZH about

URL: https://www.geeksforgeeks.org/python/how-to-copy-a-string-in-python/

⇱ How to copy a string in Python - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

How to copy a string in Python

Last Updated : 23 Jul, 2025

Creating a copy of a string is useful when we need a duplicate of a string to work with while keeping the original string intact, strings in Python are immutable which means they can't be altered after creation, so creating a copy sometimes becomes a necessity for specific use cases.

Using Slicing

Slicing ensures that a new object is created in memory. The slicing operator[:] creates a new string by extracting all characters from the original.


Output
Hello, Python!
Hello, Python!
True

Let's explore some other ways to copy a string in python

Using Assignment

Another simple way to copy a string is by assigning it to a new variable. The copy variable points to the same memory location as original and both the variables reference the same string object.


Output
Hello, Python!
Hello, Python!
True

Using str() Constructor

The str() constructor explicitly creates a new string object with the same value from an existing one. str() ensures a new object is created, the new variable points to a different memory location


Output
Python is fun!
Python is fun!
True
Comment