• 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

Please log in or register to answer this question.

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 165 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 27 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, 2020 anonymous
0 votes
2 answers 763 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
0 votes
1 answer 26 views
Problem: Which of the following function convert a string to a float in python?Explain key differences between numbers and character strings. Use built-in functions to convert between integers, floating point numbers, and strings. Types control what operations (or methods) can be performed on a given value.
asked Feb 16 Mashhoodch 9.6k points
0 votes
1 answer 28 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, 2020 Gavin 15.3k points
2 votes
1 answer 26 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, 2020 samhaz 5k points
3 votes
1 answer 28 views
Problem: Hello kodlogs, Recently while studying the string concept in python was doing the string interview preparation in that the question was about how can we accept the last 2 character of the string so I am bit confused, like how can we achieved this and what are the other way to access the last two character of string.
asked Jun 11, 2020 Gavin 15.3k points
1 vote
1 answer 133 views
Problem : I am trying to find solution for following error replace any space ' ' by '_' in 2-character string passcode. sample output for the given program:
asked Nov 11, 2019 peterlaw 6.9k points
0 votes
1 answer 2 views
Problem: How to append to a string in python ?
asked 1 day ago Ashty 1.7k points
0 votes
1 answer 5 views
Problem: Python String isnumeric() Method The isnumeric() method returns True if all the characters are numeric (0-9), otherwise False. Exponents, like ² and ¾ are also considered to be numeric values. then how check if a string is a number python
asked 5 days ago Mashhoodch 9.6k points