How to make copy of a string in Python
In this tutorial, we are going to learn about multiple ways to copy a string in Python.
Copying the string
Consider, we have the following string:
name = 'kelly'
To make a copy of a string, we can use the built-in slice syntax [:]
in Python.
Example:
name = 'kelly'
copy = name[:]
print(copy) # 'kelly'
Similarly, we can also do it by assigning a string
to the new variable.
name = 'kelly'
copy = name
print(copy) # 'kelly'
or we can use the str()
function to create a string copy.
name = 'kelly'
copy = str(name)
print(copy) # 'kelly'