python remove characters imn string that are not a certain character code example
Example 1: 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'
Example 2: python trim certain characters from string
string = ' xoxo love xoxo '
# Leading and trailing whitespaces are removed
print(string.strip())
# All <whitespace>,x,o,e characters in the left
# and right of string are removed
print(string.strip(' xoe'))
#