• Register
0 votes
24 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

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 21 views
21 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 Gavin 15.3k points
0 votes
1 answer 9 views
9 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 Mashhoodch 1.2k points
1 vote
1 answer 63 views
63 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 samhaz 5k points
0 votes
1 answer 24 views
24 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 LizzyM 6.1k points
2 votes
1 answer 28 views
28 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 Gavin 15.3k points