python - str containing whitespace characters and int(str) - python

I'm trying to write a really simple function. It should return True if given object is a digit (0-9), False otherwise. Here are examples of input and desired output:
is_digit("") => False
is_digit("7") => True
is_digit(" ") => False
is_digit("a") => False
is_digit("a5") => False
My code works for the above examples.
def is_digit(n):
try:
return int(n) in range(0, 10)
except:
return False
Trouble is, the function returns True for n = "1\n" when it should return False. So, a string like "1" should be converted to integer and is a digit, but a string like "1\n" should not, yet I don't know how to get around that. How can I account for string literals?
P.S. If my title is lame, advice on renaming it is welcome.

You don't need to define a custom function for this. There is a built-in function for this, namely isdigit().
You can use it as: "a5".isdigit() or "1/n".isdigit().In both cases it will return False.

First you have to convert your literals into string then you can apply isdigit.
You can not apply isdigit directly to number. It will throw an error
AttributeError: 'int' object has no attribute 'isdigit'
You have to typecast your number in string.
eg:
In [3]: str(0).isdigit()
Out[3]: True
or
In [1]: "0".isdigit()
Out[1]: True

Related

Why does the str.isdigit always output the opposite result

def is_valid_zip(zip_code):
"""Returns whether the input string is a valid (5 digit) zip code
"""
if (len(zip_code) == 5) and (str.isnumeric == True):
return True
else :
return False
First of all, it should be str.isnumeric() == True as that's calling the isnumeric function. Second of all you should be really using str.isdigit().
str.isnumeric()
In Python, decimal characters (like: 0, 1, 2..), digits (like: subscript, superscript), and characters having Unicode numeric value property (like: fraction, roman numerals, currency numerators) are all considered numeric characters. Therefore even japanese character for 1, 2 and 3 would pass this check.
str.isdigit()
On the other hand isdigit() will only return True if all characters in a string are digits. If not, it returns False.
source: https://www.programiz.com/python-programming/methods/string/isdigit
A few point to discuss. Regarding your condition:
str.isnumeric == True
That thing on the left side is the function itself, not a call to the function giving a result, the latter would be some_string.isnumeric().
The chances of the function object being equal to true are somewhere between zero and a very, very small number :-)
It's also redundant to compare boolean values against boolean constants since the result of the comparison is just another boolean value. Where do you stop in that case? For example:
(((some_bool_value == True) == True) == True) != False ...
Another point, the code form if cond then return true else return false can be replaced with the much less verbose return cond.
And also keep in mind that isnumeric() allows other things than raw digits, like ¾. If you just want the digits, you're probably better off with another method. You may be tempted to instead use isdigit(), but even that allows other things than just what most would consider "normal" digits, such as allowing "90²10" as a postal code, presumably the much trendier part of Beverly Hills :-).
If you only wanted the raw digits 0-9 (which is probably the case with US postal codes like you seem to be targeting), neither isnumeric() nor isdigit() is really suitable.
An implementation of the function, taking all that into account, could be as follows:
def is_valid_zip(zip_code):
if len(zip_code) != 5:
return False
return all([x in "1234567890" for x in zip_code])
it should be zip_code.isnumeric() not str.isnumeric
Also, why don't you use regex:
import re
RE_ZIP_CODE = re.compile(r'^[0-9]{5}$') # or r'^\d{5}$' for all digit characters
def is_valid_zip(zip_code):
return RE_ZIP_CODE.search(zip_code) is not None
This should work
def is_valid_zip(zip_code):
"""Returns whether the input string is a valid (5 digit) zip code
"""
if len(zip_code) == 5 and zip_code.isnumeric():
return True
else:
return False
print(is_valid_zip("98909"))

How can I remove the string element in a series in Python?

I got a series name basepay that contains both String and Numeric element. What I wanted to do is to calculate the mean of the numeric part. I've tried basepay.mean() and the kernel return TypeError: unsupported operand type(s) for +: 'float' and 'str' So I tried to drop off the non-numeric part.
I used mask = basepay.astype(str).str.isnumeric() to create a mask. But all the elements in the returning series are False.
Shouldn't it return True when the element in the basepay is like '1234.32' ?
By the way, is there a faster way to deal with this problem ?
it might be easiest to just use a try catch block inside the mask function
like
try:
float(basepay)
catch:
do something if it fails
The .isnumeric() method doesn't accept decimal characters.
The documentation states:
Return True if all characters in the string are numeric characters,
and there is at least one character, False otherwise.
. is not clasified as a 'numeric character':
>>> '.'.isnumeric()
False
What you want is not .isalpha()
The documentation states:
Return True if all characters in the string are alphabetic and there is at least one character, False otherwise.
So not .isalpha() will check if there are any alphabetical characters and will return True if there are non. In this case . is not counted as an alphabetical character.
Example:
>>> '.'.isalpha()
False
Therefore:
>>> not '1234.32'.isalpha()
True

How to convert string to Boolean in Python

I came across this question for the interview. I had to convert the string to Boolean and return the Boolean value
For ex s="3>1>5" => false
I tried using
ans= bool(s)
But I always get the answer as True even though the above example gives False if we don't pass it as a string
You must be looking for eval(string)
>>> eval("3>1>5")
False

Eliminate numbers from a sentence in Python

I am parsing from a xml file and trying to build a dictionary for every context in the xml. I have done parsing successfully, and now I need to get rid of the stopwords, punctuations and numbers from the string I get.
However, for some reason, I couldn't get rid of the numbers, I have been debugging all night, hope someone could help me with it...
def is_number(s):
try:
float(s)
return True
except ValueError:
return False
I have been checking that the method 'is_number' is working, but I don't why it still could get pass the if statement:
if (words[headIndex + index] not in cachedStopWords) and ~isNumber:
Thanks in advance!
The problem is:
~isNumber
~ is the bitwise not operator. You want the not boolean operator:
>>> ~True
-2
>>> ~False
-1
>>> not True
False
>>> not False
True
The bitwise operator will lead to ~isNumber always being a truthy value (-1 or -2), and so your if statement is entered.

General Python Programming 2.7 data validation

I was wondering what functions should I use for this def function so that the user can only enter strings and not integers -
def GetTextFromUser():
TextFromUser = raw_input('Please enter the text to use: ')
return TextFromUser
raw_input() always returns a string. But ,if by string you meant only alphabets are allowed, then you can use: str.isalpha()
S.isalpha() -> bool
Return True if all characters in S are alphabetic
and there is at least one character in S, False otherwise.
examples:
In [9]: 'foo'.isalpha()
Out[9]: True
In [10]: 'foo23'.isalpha()
Out[10]: False
According to the documentation, raw_input always returns a string. If you never want it to return an integer, I would try to convert it to an integer using int() and catch an exception if it fails. If it doesn't fail, immediately after, return the value. This follows the Pythonic style of "it's better to ask for forgiveness than for permission."

Categories

Resources