remove symbols python code example
Example 1: strip characters from string python regex
syntax
re.sub(pattern, repl, string, max=0)
#!/usr/bin/python
import re
phone = "2004-959-559 # This is Phone Number"
# Delete Python-style comments
num = re.sub(r'#.*$', "", phone)
print "Phone Num : ", num
#output Phone Num : 2004-959-559
# Remove anything other than digits
num = re.sub(r'\D', "", phone)
print "Phone Num : ", num
#output Phone Num : 2004959559
Example 2: How to get rid of extra information python
#This may not be exactly what you were looking for but this is
#something that I had trouble with and thought I'd share with everyone
string = "abcdefghi21 jklmnop"
string2 = "abcdefghi3476 jrtklghnopgghhr"
#These strings are semi-random but the goal here is in both of these
#The start is the exact same like it was in my original problem
#Our goal is to get the numbers
#I approached this like so
string_r = string.replace('abcdefghi','')
string2_r = string2.replace('abcdefghi','')
#Now in my problem I used a for loop wich I imagine you could easily
#Make this into a for loop and if not there is tutorials and
#Grepper answers for them!
#After that they will both look like so:
#string = "21 jklmnop"
#string2 = "3476 jrtklghnopgghhr"
#To finsih this I split the text and stored the [0]
string_l = string_r.split(' ')
string2_l = string2_r.split(' ')
#Save the variables
num = string_l[0]
num2 = string2_l[0]
#results:
print(num)
#num = '21'
print(num2)
#num2 = '3476'