Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/285.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 检查字符串是否包含除所需类型之外的其他字符_Python_Boolean - Fatal编程技术网

Python 检查字符串是否包含除所需类型之外的其他字符

Python 检查字符串是否包含除所需类型之外的其他字符,python,boolean,Python,Boolean,我希望能够返回一个布尔值,如果有任何其他字符认为是假的。前 x = "0&" if x.isnumeric() == False: return "not a number" else: return "is a number" 即使其中有一个数字,如果有任何其他字符不是数字,我希望它返回false。所以我希望它在“06”时返回True(只是一个例子),在“3@”时返回False(另一个例子) 此代码应打印

我希望能够返回一个布尔值,如果有任何其他字符认为是假的。前

x = "0&"
if x.isnumeric() == False:
     return "not a number"
else:
     return "is a number"
即使其中有一个数字,如果有任何其他字符不是数字,我希望它返回false。所以我希望它在“06”时返回True(只是一个例子),在“3@”时返回False(另一个例子)


此代码应打印为false

代码只有通过添加代码的作用以及如何解决问题的说明才能改进答案。我建议编辑答案。另外,这也有很大的效率问题:1/您应该直接迭代字符串中的字符,而不是使用索引(在Python中通常应该避免使用索引),因此您应该使用
作为文本中的字符:
。2/即使第一个字符不是数字,代码也会对整个字符串进行迭代。在这种情况下,您应该
打印(“不是数字”)
立即中断
text ="332a"
bool_last=True
for n in range(0,len(text)):
 bool1=text[n].isdigit()
 bool_last=bool1 & bool_last
print(bool_last)
x="8R"    
if x.isnumeric():
     print("True")
else:
     print("False")