• Register
1 vote
1.2k views

Problem:

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

Answer:

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 203 views
203 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
1 answer 11 views
11 views
Problem: How to I Solve this: python convert string to list of chars HELP! I need help understanding this: python convert string to list of chars Can someone please help solve this, I am new in python(). Someone have idea to solve this thanks!
asked Mar 7 Mashhoodch 13k points
0 votes
1 answer 1 view
1 view
Question: I know that in Java I can extract a substring from a String object doing something like: String string= "Hello World"; String subString = string.substring(5); And in this wat the subString variable will contain only the Hello string and I know that I can also specify ... two initial characters. So for example I have: String value = "12345" and my substring have to be 345 How can I do it?
asked Apr 12 Atik03 26.5k points
0 votes
2 answers 15 views
15 views
Problem: Do you have any suggestions about how I can resolve this? Find maximum of minimum for every window size in a given array
asked Mar 29 tuhin1 48.9k points
0 votes
1 answer 13 views
13 views
Problem: I wrote the following Java code, and it returned a time out error. I'm not exactly sure what that means nor why the code doesn't run public int countHi(String str) { int pos = str.indexOf("hi"); int count = 0; while(pos!=-1) { count++; pos = str.substring(pos).indexOf("hi"); } return count; } I know an alternative solution, using a for loop but I really thought this would work too.
asked Apr 25 Humaira ahmed 50.7k points
0 votes
2 answers 1K views
1K 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 23 views
23 views
Problem: Does strcmp() work through each index of two arrays of characters and if the ASCII value is smaller at an identical index of two strings, that string is there alphabetically greater and therefore a 0 or 1 or 2 is returned?
asked Apr 11 ummesalma 29.2k points
0 votes
1 answer 5 views
5 views
Problem: If some one want to remove the last 4 characters of a string how to do that ? Remove last two characters from string python.
asked Mar 22 PkGuy 23.5k points
0 votes
1 answer 17 views
17 views
Problem: How can I fix this: get last two characters of string python
asked Mar 15 Wafa Abu Yousef 6.1k points
3 votes
1 answer 31 views
31 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