In this Answer, we’ll learn how to use Python to take a word as input and output it with doubled letters.
Input: red
Output: rreedd
To solve this problem, we’ll use the repetition and concatenation properties of strings.
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)
hello
in the variable string
.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)
hello
in the variable string1
.world
in the variable string2
.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.
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*2print(output)
Enter the input below
input_string
.""
in the variable output
.for
loop.i
with 2
and add it to the variable output
. Store the result in the variable output
.output
variable.