by Sai gowtham

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'

Top Udemy Courses

JavaScript - The Complete Guide 2020 (Beginner + Advanced)
JavaScript - The Complete Guide 2020 (Beginner + Advanced)
45,614 students enrolled
52 hours of video content
View Course
React - The Complete Guide (incl Hooks, React Router, Redux)
React - The Complete Guide (incl Hooks, React Router, Redux)
284,472 students enrolled
40 hours of video content
View Course
Vue - The Complete Guide (w/ Router, Vuex, Composition API)
Vue - The Complete Guide (w/ Router, Vuex, Composition API)
130,921 students enrolled
48.5 hours of video content
View Course