How to remove all occurrences of a character in a string with Python

3 Answers

0 votes
def remove_all_occurrences(s, ch): 
    total_chars = s.count(ch) 
   
    s = list(s) 
   
    while total_chars: 
        s.remove(ch) 
        total_chars -= 1
 
    s = '' . join(s)
       
    return s
       
 
s = "python programming version 3.2"

s = remove_all_occurrences(s, 'o') 
     
print(s)
     
     
'''
run:
 
pythn prgramming versin 3.2
 
'''

 



answered Jan 31, 2019 by avibootz
edited Nov 14, 2023 by avibootz
0 votes
s = "python java php pascal"
   
removeChar = "a"
   
s = s.replace(removeChar, "") 
    
print(s) 
       
  
         
'''
run:
  
python jv php pscl
         
'''

 



answered Nov 14, 2023 by avibootz
0 votes
def remove_all_occurrences(s, ch): 
    new_string = ''
    for character in s:
        if character != ch:
            new_string += character
    
    return new_string
        
  
s = "python programming version 3.2"
 
s = remove_all_occurrences(s, 'o') 
      
print(s)
      
      
'''
run:
  
pythn prgramming versin 3.2
  
'''

 



answered Nov 14, 2023 by avibootz
...