• Register
0 votes
35 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 29 views
29 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 13 views
13 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 9.3k points
1 vote
1 answer 99 views
99 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
2 votes
1 answer 32 views
32 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
2 answers 38 views
38 views
Problem: I am a newbee in python programming. I tried to remove all occurrences of a character in a list python. So I wrote a code: Nums = [11, 22, 11, 33, 11, 44, 11, 55] a = 11 print ("Original Numbers:") print (Nums) i=0 length = len(Nums) while(i< ... line 8, in <module> if(Nums[i]==a): IndexError: list index out of range Why I am getting this output but not the actual result. Please help
asked Mar 22, 2020 LizzyM 6.1k points
0 votes
1 answer 34 views
34 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 23 views
23 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
0 votes
1 answer 7 views
0 votes
1 answer 28 views
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
0 votes
1 answer 2 views
2 views
Problem: Why I got the error : remove first and last character from string python Any ideas on what I need to do to fix this? Thanks! Can someone please help fix this, remove first and last character from string python
asked 3 hours ago Mashhoodch 9.3k points