Use strip('%')
, as:
In [9]: "99.5%".strip('%')
Out[9]: '99.5' #convert this to float using float() and divide by 100
In [10]: def p2f(x):
return float(x.strip('%'))/100
....:
In [12]: p2f("99%")
Out[12]: 0.98999999999999999
In [13]: p2f("99.5%")
Out[13]: 0.995
More Related Contents:
- Most pythonic way to interleave two strings
- Oops, try again. Your function failed on the message yes. It returned 'yes' when it should have returned 'Shutting down'
- How to match a string to pattern “Foo-Bar”, where Bar can be any element of a list? [closed]
- Searching for IP addresses in a file
- Swapping variable places inside if throws an error
- Unable to use convert strings into integers for a for loop
- How to use string.replace() in python 3.x
- Behavior of exec function in Python 2 and Python 3
- Python 2.7 getting user input and manipulating as string without quotations
- Could not find a version that satisfies the requirement tensorflow
- How to extract text from an existing docx file using python-docx
- Getting only element from a single-element list in Python?
- Why does this not work as an array membership test? [duplicate]
- Using multiple Python engines (32Bit/64bit and 2.7/3.5)
- How can I convert 24 hour time to 12 hour time?
- How do I change the string representation of a Python class? [duplicate]
- Removing control characters from a string in python
- Python regex for unicode capitalized words
- How to get Windows short file name in python?
- What does the “yield” keyword do in Python?
- “TypeError: a bytes-like object is required, not ‘str'” when handling file content in Python 3
- How to use collections.abc from both Python 3.8+ and Python 2.7
- How can I use f-string with a variable, not with a string literal?
- Byte Array to Hex String
- String-based enum in Python
- Switch between python 2.7 and python 3.5 on Mac OS X
- Download YouTube video using Python to a certain directory
- Finding the position of a word in a string
- Why are str.count(”) and len(str) giving different outputs when used on an empty string?
- ‘is’ operator behaves unexpectedly with floats