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,880 questions

51,806 answers

573 users

How to extract only characters from a string in Python

4 Answers

0 votes
import re 

s = '#Py23thon#$% Prog(_)*&ramming 123Langu9837age'
  
result = " ".join(re.split("[^a-zA-Z]*", s)) 

print(result)  

         
      
'''
run:
 
  P y  t h o n  P r o g  r a m m i n g  L a n g u  a g e 
      
'''

 



answered Apr 24, 2020 by avibootz
0 votes
import re 

s = '#Py23thon#$% Prog(_)*&ramming 123Langu9837age'
  
result = " ".join(re.findall("[a-zA-Z]+", s)) 

print(result)  

         
      
'''
run:
 
Py thon Prog ramming Langu age
      
'''

 



answered Apr 24, 2020 by avibootz
0 votes
import re 

s = '#PY23thon#$% Prog(_)*&raMMing 123LAngu9837age'
  
result = [ch for ch in s if ch.isalpha()] 

print(result)  

         
      
'''
run:
 
['P', 'Y', 't', 'h', 'o', 'n', 'P', 'r', 'o', 'g', 'r', 'a', 'M', 'M', 
'i', 'n', 'g', 'L', 'A', 'n', 'g', 'u', 'a', 'g', 'e']
      
'''

 



answered Apr 24, 2020 by avibootz
0 votes
def extract_characters(s):  
    length = len(s) 
    
    chars = ""
    for i in range(0, length):  
        ch = s[i]  
        if ch.isalpha():  
            chars = chars + ch  
            
    return chars
        
       
s = "c++14$c#.net&%java*() php <>/python 3.7.3"
        
print(extract_characters(s))  
  
  
  
'''
run:
  
ccnetjavaphppython
  
'''

 



answered Apr 13, 2024 by avibootz
...