GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing codescracker.com — Python Program to Copy String
Python Program to Copy StringThis 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 CharacterTo 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: Now supply the input say codescracker as string, and then press 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 IndexingThis 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: 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 = OperatorThis 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: Copy String using String SlicingThis 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« Previous Program Next Program » Practice Quizzes
Test your programming and computer science knowledge with interactive MCQs and topic-based quizzes. Popular Topics
Explore the most popular programming and computer science tutorials on CodesCracker. |
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |