I am reading in a string from a file which has the format $ 235.0 M. I need to convert this to a number and save the letter M somewhere else. Any pointers on how to strip off the $ and M?
Thank you.
I am reading in a string from a file which has the format $ 235.0 M. I need to convert this to a number and save the letter M somewhere else. Any pointers on how to strip off the $ and M?
Thank you.
Hint:
data='$ 235.0 M'
print data.lstrip('$ ')
print data.rstrip(' M')
Here is an example. Remember that after reading from a text-file, (if you read the whole line) you may get newline characters ("\n"). Also, you need to remove the spaces.
n = '$ 235.0 M' #Example string
n = n.strip('$M ') #Cut off the chars - including spaces!
n = float(n) #Must convert to int because of the '.0' ('235.0')
##################
print n * 2 #Just to test if it works
Enjoy
Sorry Tony, I got distracted after I started writing a reply and your post didn't show up until I had added mine!
Thanks guys.
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.