Character repeating in a string
Question:
can you please tell me how to get every character in a string repeated using the for loop in python?my progress is as far as :
def double(str):
for i in range(len(str)):
return i * 2
and this returns only the first letter of the string repeated
Answers:
I believe you want to print each character of the input string twice, in order. The issue with your attempt is your use of return
. You only want to return the final string, not just a single repetition of one character from inside the loop. So you want your return statement to be somehow outside the for-loop.
Since this sounds like a homework problem, try to figure out how to do that above before continuing below.
Still stuck?
Print each character in a string twice, in order:
def double(str):
outstr = ''
for character in str:
outstr = outstr + character + character
return outstr
Without using a for-loop:
def double(str):
return ''.join([c+c for c in str])
With minimal modification to existing code:
(range(len))
would iterate over indices, not actual chars, and yield
instead of return
makes it a generator thus allowing to return all values)
def double(str):
for i in str:
yield i * 2
Use it as so:
str = 'This is a test'
''.join(double(str))
General variation, which returns string, repeating each character in the original string n times:
def multichar(text, n):
return ''.join([x * n for x in text])
Explanation:
''.join(...)
– glues everything in (…) together into one piece without spaces, i.e. ''.join('a', 'b', 'c')
will return 'abc'
.
' '.join('a', 'b', 'c')
will return 'a b c'
.
'-'.join('a', 'b', 'c')
will return 'a-b-c'
.
…you get the idea 🙂
[x * n ...]
– repeats x
n
times, i.e. 'a' * 3
gives 'aaa'
.
[... for x in text]
– normal for
loop – goes over every element in text
.
Plain English for what the last line of the code does:
Glue without spaces every element in the list, generated by repeating x
n
times for every x
in text
.
Example:
>>> multichar('Hello', 2)
will return:
>>> 'HHeelllloo'
given a string, the below code repeats every original character in the string twice with the help of functions and while loop.
res is a list that stores the doubled characters of the string. ” “.join() is called to connect the list.
the number of times that the original character is repeated can be changed.
here’s the code snippet.
def paper_doll(text):
j = len(text)
i=0
res=[]
while i<j :
res.append(text[i]*2)
i = i+1
result ="".join(res)
print(result)
paper_doll(‘paperdoll’)
OUTPUT :
ppaappeerrddoollll
open to any kind of feedback and suggestions.
str = input('Enter string: ')
char = " "
for i in char:
char = char + i + i
print(char)
def time_3(letter):
a = []
for i in range(0,len(letter)):
a.append(letter[i]*3)
return ''.join(a)
# call the function
print(time_3('HELLO'))
Output:
HHHEEELLLLLLOOO
can you please tell me how to get every character in a string repeated using the for loop in python?my progress is as far as :
def double(str):
for i in range(len(str)):
return i * 2
and this returns only the first letter of the string repeated
I believe you want to print each character of the input string twice, in order. The issue with your attempt is your use of return
. You only want to return the final string, not just a single repetition of one character from inside the loop. So you want your return statement to be somehow outside the for-loop.
Since this sounds like a homework problem, try to figure out how to do that above before continuing below.
Still stuck?
Print each character in a string twice, in order:
def double(str):
outstr = ''
for character in str:
outstr = outstr + character + character
return outstr
Without using a for-loop:
def double(str):
return ''.join([c+c for c in str])
With minimal modification to existing code:
(range(len))
would iterate over indices, not actual chars, and yield
instead of return
makes it a generator thus allowing to return all values)
def double(str):
for i in str:
yield i * 2
Use it as so:
str = 'This is a test'
''.join(double(str))
General variation, which returns string, repeating each character in the original string n times:
def multichar(text, n):
return ''.join([x * n for x in text])
Explanation:
''.join(...)
– glues everything in (…) together into one piece without spaces, i.e. ''.join('a', 'b', 'c')
will return 'abc'
.
' '.join('a', 'b', 'c')
will return 'a b c'
.
'-'.join('a', 'b', 'c')
will return 'a-b-c'
.
…you get the idea 🙂
[x * n ...]
– repeats x
n
times, i.e. 'a' * 3
gives 'aaa'
.
[... for x in text]
– normal for
loop – goes over every element in text
.
Plain English for what the last line of the code does:
Glue without spaces every element in the list, generated by repeating x
n
times for every x
in text
.
Example:
>>> multichar('Hello', 2)
will return:
>>> 'HHeelllloo'
given a string, the below code repeats every original character in the string twice with the help of functions and while loop.
res is a list that stores the doubled characters of the string. ” “.join() is called to connect the list.
the number of times that the original character is repeated can be changed.
here’s the code snippet.
def paper_doll(text):
j = len(text)
i=0
res=[]
while i<j :
res.append(text[i]*2)
i = i+1
result ="".join(res)
print(result)
paper_doll(‘paperdoll’)
OUTPUT :
ppaappeerrddoollll
open to any kind of feedback and suggestions.
str = input('Enter string: ')
char = " "
for i in char:
char = char + i + i
print(char)
def time_3(letter):
a = []
for i in range(0,len(letter)):
a.append(letter[i]*3)
return ''.join(a)
# call the function
print(time_3('HELLO'))
Output:
HHHEEELLLLLLOOO