Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,651 questions

51,528 answers

573 users

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

Related questions

...