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
...