• Register
1 vote


I am new in python programming and I have been doing a course on it as a new comer I tried to repeat some char in a string. I wrote a code :

def Work(str):
    for i in range(len(str)):
        return i * 2

But it does not work . how can I do this?

10 7 2
6,060 points

1 Answer

1 vote


The main reason your code is not working may be your return function. If you want to repeat char in string please use this code:

def Work(str):
    allin = ''
    for character in str:
        allin = allin + char + char 
    return allin

If you dont want to use loops you can use:

def Work(str):
    return ''.join([c+c for c in str])

I hope this may help you the most. If you still do not understand please fell free to ask me anything you want to know about this thing.

Happy Coding.

11 6 4
34,950 points
but char is not defined here .

Related questions

0 votes
1 answer 112 views
Problem : I am trying to print every string in Reverse Double Character way (e.g. string -> ggnniirrttss) import java.util.Scanner; public class ReverseDoubleCharecters { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.println("Enter ... I get as follows: ss tt rr ii nn gg All double characters are printed on new lines. Anybody has the solution on this?
asked Nov 22, 2019 peterlaw 6.9k points
0 votes
0 answers 19 views
I have a text file that consists of similar lines and few are half similar to other lines in a text file. Input.txt I would like to play: Volleyball I would like to play: Volleyball I would like to play: TableTennis I would like to play: Baseball I do not know how ... collected[key]}\n') I can print only the second line. Don't know where I am going wrong. Any help would be appreciated. Thank you
asked Jul 16 anonymous
0 votes
1 answer 19 views
Problem: Hello kodlogs, Recently while attempting, the problem in the python there is a error that came can you please help sort out this problem, How can I get the first character from the first string in Python?,It seems that I could use list[0][1:] but that does not give me the first character of the string that I am looking for. mysrt = "sdf" >>> mystr[0][1:]
asked Jun 6 Gavin 15.3k points
2 votes
1 answer 12 views
Problem: I was trying to find the first repeated word in a string in python using dictionary. So I wrote this code which give me error. def whichRepeat(input): words = input.split(' ') dict = words for key in words: if dict[key]>1: print (key) return if __name__ == ... "main.py", line 10, in whichRepeat if dict[key]>1: TypeError: list indices must be integers or slices, not str How to solve this?
asked Apr 28 samhaz 5k points
0 votes
2 answers 414 views
Problem : I am facing following issue need solution return true if the string "cat" and "dog" appear the same number of times in the given string
asked Nov 13, 2019 peterlaw 6.9k points