Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

python

How to double each letter of a word in Python

Raghav Aggarwal

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Double each letter of a word in Python

In this Answer, we’ll learn how to use Python to take a word as input and output it with doubled letters.

Example

Input: red

Output: rreedd

Solution

To solve this problem, we’ll use the repetition and concatenation properties of strings.

String repetition

In Python, any string when multiplied with a numeric value results in a repetition of the original string.

For example, when a string hello is multiplied by a number 2, the result is hellohello.

string = "hello"
print(string*2)
String Repetition

Explanation

  • Line 1: We store the string hello in the variable string.
  • Line 2: We print the result of the multiplication of a string with an integer.

String concatenation

When a string is added with another string, it results in the concatenation of both the strings.

For example, when a string hello is added to the string world, it results in the output helloworld.

string1="hello"
string2="world"
print(string1+string2)
String Concatenation

Explanation

  • Line 1: We store the string hello in the variable string1.
  • Line 2: We store the string world in the variable string2.
  • Line 2: We print the result of the addition of a string with another string.

We need to repeat individual characters in the given problem instead of the whole word. To solve this, we will use both string repetition and concatenation together.

Solution

Take the input string and store it in a variable. Loop through the string using a for loop, multiply every character by 2, and add the resulting character to an output variable holding an empty string initially. This will double every character and add all the resulting characters to form an expected output string.

input_string = input()
output=""
for i in input_string:
output = output + i*2
print(output)

Enter the input below

Solution

Explanation

  • Line 1: We store the input string in the variable input_string.
  • Line 2: We store an empty string "" in the variable output.
  • Line 3: We iterate through the string using a for loop.
  • Line 4: We multiply each character stored in the variable i with 2 and add it to the variable output. Store the result in the variable output.
  • Line 5: We print the output variable.

RELATED TAGS

python

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring