codescracker
python

Python Program to Copy String



« Previous Program Next Program »

This article is created to cover some programs in Python, to copy one string to another. Here are the list of approaches used to do the task of copying string entered by user at run-time:

Copy String Character by Character

To copy one string to another in Python, you have to ask from user to enter a string, then copy that string to another as shown in the program given below. The question is, write a Python program to copy string in character by character manner. Here is its answer:

print("Enter the String: ")
textOne = input()

textTwo = ""
for x in textOne:
    textTwo = textTwo + x

print("\nOriginal String =", textOne)
print("\nCopied String =", textTwo)

Here is its sample run:

copy string python

Now supply the input say codescracker as string, and then press ENTER key to copy the given string into another string, and print the value of both (original and copied) strings as shown in the snapshot given below:

copy string program python

In above program, the following block of code:

for x in textOne:
    textTwo = textTwo + x

states that, each time the character gets copied to x and added with textTwo, then initialized the addition result as new value of textTwo. For example, if user enters codescracker as string, then dry run goes like:

Copy String using for Loop and Indexing

This program does the same job as of previous one. But it is created with different approach. This program uses index of entered string to copy its value to another string. Let's have a look at the program first:

print("Enter the String: ", end="")
sOne = input()

sTwo = ""
for i in range(len(sOne)):
    sTwo = sTwo + sOne[i]

print("\nOriginal String =", sOne)
print("\nCopied String =", sTwo)

Here is its sample run with user input, Welcome to codescracker.com:

python copy string

Note - The end= is used to skip inserting an automatic newline using print().

Note - The len() method returns length of string passed as its argument.

Indexing starts with 0. For example, if user enters a string say codes and this string gets initialized to a variable say sOne. That is, sOne = "codes". Then sOne[0] refers to "c" (first character of "codes"), sOne[1] refers to "o" (second character of "codes"), and so on

Therefore in similar way like done in previous program, this program also copies the string in character by character manner. The dry run of following block of code:

for i in range(len(sOne)):
    sTwo = sTwo + sOne[i]

with same user input, as provided in above sample run, that is Welcome to codescracker.com goes like:

Copy String using = Operator

This is the simplest program to copy one string to another just by using = or initialization operator. Previous two programs are provided for practicing purpose to improve coding skills in Python.

print("Enter String: ", end="")
sOne = input()

sTwo = sOne
print("\nCopied String =", sTwo)

Here is its sample run with string input Hello Python:

python copy one string to another

Copy String using String Slicing

This program does the same job, using string slicing. That is, I've sliced all the characters of entered string by user to another string.

print("Enter String: ", end="")
sOne = input()

sTwo = sOne[:]
print("\nCopied String =", sTwo)

Note - While slicing sub-string from a string using [:], empty before : (colon) treated as 0, whereas empty after : (colon) treated as length value of string. Therefore from 0th to last index, all characters gets sliced and copied to sTwo variable.

Same Program in Other Languages

Python Online Test


« Previous Program Next Program »



© Copyright 2021. All Rights Reserved.

CodesCracker