Copy String in Python

  1. Use an Empty String to Get a Copy String in Python
  2. Use Slicing to Copy a String in Python
  3. Use the str() Function to Copy a String in Python
  4. Use String Formatting to Copy a String in Python

This tutorial will introduce different methods to copy a string in Python.

Before we move on to the different methods to copy a string in Python, we should keep in mind that a string cannot be directly copied.

In Python, strings are immutable, meaning that their value cannot change over the course of the program. Being immutable also means that a string cannot directly have a copy.

If a new variable is declared and is directly assigned the value of a given string variable, this would not create a copy of the original string. Instead, both of the created variables would point to the same string.

However, there are a few loopholes that can be utilized to copy a string in Python. We will discuss all such methods below in this article.

Use an Empty String to Get a Copy String in Python

We start with the simplest method, which is very easy to implement. We need to add an empty string to the original string using the concatenation operator while declaring the new string.

The following code uses an empty string to get a copy string in Python.

ostr = 'Web'
nstr = '' + ostr
print(nstr)

The above code provides the following output:

Web

Use Slicing to Copy a String in Python

The slice or the : operator can be utilized to slice the original and generate a new one. The slicing operator works by taking a start and an index value in its syntax.

If both these values are not passed, the original string would be copied intact to the new variable.

The following code uses slicing to copy a string in Python.

ostr = 'Web'
nstr = ostr[:]
print(nstr)

The above code provides the following output:

Web

Use the str() Function to Copy a String in Python

The str() function, when made to pass a given string as its argument, returns the original string itself. This can be utilized when we have to create a copy string.

The following code uses the str() function to copy a string in python.

ostr = 'Web'
nstr = str(ostr)
print(nstr)

The above code provides the following output:

Web

Use String Formatting to Copy a String in Python

String formatting supplies a wide variety of customizations for the user to select from in the python code. The % sign is commonly implied as the interpolation operator and is utilized to implement string formatting.

Although there is more than one way to implement string formatting in Python, the interpolation operator or % sign is very versatile. It works on all available versions of Python and is the oldest of the lot.

The % sign, along with a letter representing the conversion type, is marked as a placeholder for the variable.

The following code uses string formatting to copy a string in Python.

ostr = 'Web'  
nstr = '%s' % ostr
print(nstr)

The above code provides the following output:

Web

Although copying a string can be achieved indirectly in Python, it is not essentially a recommended practice. The copy module does not work properly on the string.

Moreover, these strings cannot really be mutated in any way; therefore, creating a copy is pretty much a waste of time and system memory.

Contribute
DelftStack is a collective effort contributed by software geeks like you. If you like the article and would like to contribute to DelftStack by writing paid articles, you can check the write for us page.

Related Article - Python String

  • tostring() Equivalent in Python
  • Check Whether a String Contains Substring in Python