如何识别字符串中的单个字符

时间:2021-05-07 21:50:12

I'm trying to create a function that returns 'true' if a inputted string has no numbers in it and 'false' if it has any numbers in it

我正在尝试创建一个函数,如果输入的字符串中没有数字则返回'true',如果其中有任何数字则返回'false'

ex:

例如:

'Ohio' = true , 'agent 007' = false

'Ohio'= true,'agent 007'= false

So far I've tried

到目前为止,我已经尝试过了

numbers = '0123456789'
Lowercase = 'abcdefghijklmnopqrstuvwxyz'
Uppercase = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'

def alphabet_only(a_string):
    if a_string[:] not in numbers:
        print('true')
    else:
        print('false')

the problem is that i cant figure out how to identify a single number character by itself in a string to label it as false.

问题是我无法弄清楚如何在字符串中单独识别单个数字字符以将其标记为false。

so far it will return

到目前为止它将回归

6 = false (good)

6 =假(好)

54 = true (bad)

54 =真(坏)

pie = true (good)

pie = true(好)

pie18 = true (bad)

pie18 =真(坏)

6 个解决方案

#1


8  

Use isalpha() from the standard library

使用标准库中的isalpha()

>>> test='test1'
>>> test.isalpha()
>>> False

#2


3  

Have you considered using regular expressions for this?

您是否考虑过使用正则表达式?

import re

def has_no_numbers(string):
    return re.search('[0-9]', string) is None

#3


3  

In order to test if a string contains numbers, you have varied options

为了测试字符串是否包含数字,您有多种选择

  1. Use Regular expressions
  2. 使用正则表达式
  3. Use set intersection set(string.digits).intersection(st)) < 9
  4. 使用set intersection set(string.digits).intersection(st))<9
  5. Use str.translate. len(st.translate(None, string.digits)) < len(st)
  6. 使用str.translate。 len(st.translate(None,string.digits)) (st)

But all of the above is an overkill because you just need to ensure that one of the characters is a digit. It does not matter which digit or the number of occurrence. In such a case using str.isdigit check and using any short circuit boolean evaluation should be the right approach.

但是所有这些都是一种矫枉过正,因为你只需要确保其中一个字符是一个数字。哪个数字或出现次数无关紧要。在这种情况下使用str.isdigit检查并使用任何短路布尔评估应该是正确的方法。

>>> def has_numbers(st):
    return any(e.isdigit() for e in st)

>>> has_numbers('agent 007')
True
>>> has_numbers('agent')
False

#4


1  

With set-intersection:

使用集合交集:

digits = set("0123456789")

def alphabet_only(a_string):
   if set(a_string) & digits:
       print ("false")
   else:
       print ("true")

#5


0  

I think you could do as follows:

我想你可以这样做:

import string

def alphabet_only(a_string):
    if all(v not in string.digits for v in a_string):
        print('true')
    else:
        print('false')


alphabet_only("6") # false        
alphabet_only("54") # false  
alphabet_only("pie") # true       
alphabet_only("pie18") #  false 
alphabet_only("Ohio")  # true       
alphabet_only("agent 007") # false  

#6


0  

There's actually a simpler way than the prevoiusly proposed:

实际上比以前提出的方法更简单:

if you do this:

如果你这样做:

list(string123)

you'll get something like this

你会得到这样的东西

['s','t','r','i','n','g','1','2','3']

Now all you need to do is set up a simple for loop to check each entry (not tested):

现在您需要做的就是设置一个简单的for循环来检查每个条目(未测试):

some_list = list(string123)
for i in range(len(some_list)):
    if some_list[i] == 1 or some_list[i] == 2 or ..... or some_list[i] == 9:
        print 'False'
    else:
        print 'True'

or as pointed out by another user the if condition can be changed for the following:

或者如其他用户所指出的,可以针对以下内容更改if条件:

0 <= some_list[i] <= 9:

And thus make the code more compact.

从而使代码更紧凑。

Here's a more beautiful version of the code:

这是一个更漂亮的代码版本:

for c in 'string123':
    0 <= c <= 9:
        print 'False'
    else:
        print 'True'

#1


8  

Use isalpha() from the standard library

使用标准库中的isalpha()

>>> test='test1'
>>> test.isalpha()
>>> False

#2


3  

Have you considered using regular expressions for this?

您是否考虑过使用正则表达式?

import re

def has_no_numbers(string):
    return re.search('[0-9]', string) is None

#3


3  

In order to test if a string contains numbers, you have varied options

为了测试字符串是否包含数字,您有多种选择

  1. Use Regular expressions
  2. 使用正则表达式
  3. Use set intersection set(string.digits).intersection(st)) < 9
  4. 使用set intersection set(string.digits).intersection(st))<9
  5. Use str.translate. len(st.translate(None, string.digits)) < len(st)
  6. 使用str.translate。 len(st.translate(None,string.digits)) (st)

But all of the above is an overkill because you just need to ensure that one of the characters is a digit. It does not matter which digit or the number of occurrence. In such a case using str.isdigit check and using any short circuit boolean evaluation should be the right approach.

但是所有这些都是一种矫枉过正,因为你只需要确保其中一个字符是一个数字。哪个数字或出现次数无关紧要。在这种情况下使用str.isdigit检查并使用任何短路布尔评估应该是正确的方法。

>>> def has_numbers(st):
    return any(e.isdigit() for e in st)

>>> has_numbers('agent 007')
True
>>> has_numbers('agent')
False

#4


1  

With set-intersection:

使用集合交集:

digits = set("0123456789")

def alphabet_only(a_string):
   if set(a_string) & digits:
       print ("false")
   else:
       print ("true")

#5


0  

I think you could do as follows:

我想你可以这样做:

import string

def alphabet_only(a_string):
    if all(v not in string.digits for v in a_string):
        print('true')
    else:
        print('false')


alphabet_only("6") # false        
alphabet_only("54") # false  
alphabet_only("pie") # true       
alphabet_only("pie18") #  false 
alphabet_only("Ohio")  # true       
alphabet_only("agent 007") # false  

#6


0  

There's actually a simpler way than the prevoiusly proposed:

实际上比以前提出的方法更简单:

if you do this:

如果你这样做:

list(string123)

you'll get something like this

你会得到这样的东西

['s','t','r','i','n','g','1','2','3']

Now all you need to do is set up a simple for loop to check each entry (not tested):

现在您需要做的就是设置一个简单的for循环来检查每个条目(未测试):

some_list = list(string123)
for i in range(len(some_list)):
    if some_list[i] == 1 or some_list[i] == 2 or ..... or some_list[i] == 9:
        print 'False'
    else:
        print 'True'

or as pointed out by another user the if condition can be changed for the following:

或者如其他用户所指出的,可以针对以下内容更改if条件:

0 <= some_list[i] <= 9:

And thus make the code more compact.

从而使代码更紧凑。

Here's a more beautiful version of the code:

这是一个更漂亮的代码版本:

for c in 'string123':
    0 <= c <= 9:
        print 'False'
    else:
        print 'True'