• Register
0 votes
40 views

Problem:

I was trying to remove all occurrences of a character in a string python but I failed. The code I wrote is:

def DeletChar(e, b) : 
      
    counts = e.count(b) 
     
    e = list(e) 
       
    while counts : 
                 
        e.remove(b) 
  
        counts -= 1
   
 
      
    print(e) 
  
if __name__ == '__main__' : 
      
    e = "geeksforgeeks"
    DeletChar(e,'g') 

The output is:

['e', 'e', 'k', 's', 'f', 'o', 'r', 'e', 'e', 'k', 's']

how to solve this problem I am a newbee please help

10 7 2
6,060 points

Please log in or register to answer this question.

1 Answer

0 votes
 
Best answer

Answer:

Your code should be like 

def DeletChar(e, b) : 
      
    counts = e.count(b) 
     
    e = list(e) 
       
    while counts : 
                 
        e.remove(b) 
  
        counts -= 1
   
    e = '' . join(e) 
      
    print(e) 
  
if __name__ == '__main__' : 
      
    e = "geeksforgeeks"
    DeletChar(e,'g') 

If you use this code your output should be like:

eeksforeeks

I used :

.join

method to get them out of list.

Happy Coding.

11 6 4
34,950 points

Related questions

2 votes
1 answer 43 views
43 views
Problem: Hello guys, I hope you all are doing well. I am learning java programming in my school. My teacher taught us many things about the string in the last class and gave a problem to solve. I tried to solve the problem but couldn’t make it happen so far. ... problem I’d be grateful to him/her. So, my problem is: find all occurrences of a substring in a string java Thanks, mate.
asked Mar 25, 2020 Gavin 15.3k points
0 votes
1 answer 24 views
24 views
Problem: The question is to traverse given string, as soon as we find an occurrence of we keep traversing until we find a character which is not c. We also set a flag to indicate that one more occurrences of c are seen. If we see c again and flag is set, then we return false.
asked Nov 2, 2020 Mashhoodch 13k points
2 votes
1 answer 39 views
39 views
Problem: Hello good soul, I have passed a successful year in java programming and now started learning python very recently. In java, I used the array.length function to find out the string length. What is the best practice in python to count a String length? Thanks for your answer. I really appreciate your effort.
asked Mar 10, 2020 Gavin 15.3k points
0 votes
1 answer 9 views
9 views
Problem: how to I Solve this: error: count number of characters in string python HELP! I need help understanding this: count number of characters in string python Can someone please help solve this, I am new in python(). Someone have idea to solve this thanks!
asked Mar 11 Mashhoodch 13k points
1 vote
1 answer 2 views
2 views
Problem: I am having an issue ,will some body help me here? Find number of occurrences of a character in a string python.
asked 6 days ago PkGuy 23.5k points
0 votes
1 answer 2 views
2 views
Problem: Help me to write a java program to count the number of words in a string.
asked 1 day ago Irenka Kiri 5.2k points
0 votes
1 answer 6 views
6 views
Problem: Hello ... Can anyone tell me that why I got this error? program to count number of characters in a string in c
asked Mar 25 Ifra 36.4k points
0 votes
1 answer 41 views
41 views
How to get the number from the occurences of individual characters Supose the string is: x = 'cdfghcfonccn' How can i solve this problem.
asked Mar 10, 2020 LizzyM 6.1k points
1 vote
1 answer 120 views
120 views
Problem: I wrote this code to find all occurrences of a substring in a string python: import re test_str = "all in all is not the all of all " test_sub = "all" print("The main string is : " + test_str) print("The substring to find : " + test_sub) print(" ... is not defined The first two ouput of this code is right but it throws error message after that. What should I do to run this code properly?
asked Apr 22, 2020 samhaz 5k points
1 vote
1 answer 24 views
24 views
Problem: I tried to count occurrences of individual character of string using python. So I wrote this program for the string: a = 'yanwawny' The program: ​​​string = 'yanwawny' ​dict((letter, string.count(letter)) for letter in set(a) But it throws the error message: File "main.py", line 1 ​a = 'yanwawny' ^ SyntaxError: invalid character in identifier What to do to solve this thing?
asked May 4, 2020 samhaz 5k points