简体   繁体   English

查找给定字符串是否是python中的单词,字符或数字

[英]finding if a given string is a word, a character or a number in python

Given this string 鉴于此字符串

random_string= '4'

i want to determine if its an integer, a character or just a word i though i could do this 我想确定它是一个整数,一个字符还是一个单词我虽然可以做到这一点

test = int(random_string)
isinstance(test,int) == True

but i realized if the random_string does not contain a number i will have an error 但我意识到如果random_string不包含数字我将有一个错误

basically the random_string can be of the forms 基本上random_string可以是表单

random_string ='hello'
random_string ='H'
random_string ='r'
random_string ='56'

anyone know a way to do this, kind of confused, for determine is its a character what i did was 任何人都知道这样做的方式,有点困惑,因为确定它是我所做的一个角色

chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
random_string in chars == True

i did another string to check if it was a lowercase letter. 我做了另一个字符串来检查它是否是一个小写字母。 also to check if its a word, i took the length of the string, if the length is more than one i determine that it is a word or a number 还要检查它是否是一个单词,我取了字符串的长度,如果长度超过一个我确定它是一个单词或一个数字

issue is how can i check if its a word or a number 问题是如何检查它的单词或数字

please help 请帮忙

Strings have methods isalpha and isdigit to test if they consist of letters and digits, respectively: 字符串有方法isalphaisdigit来测试它们是否分别由字母和数字组成:

>>> 'hello'.isalpha()
True
>>> '123'.isdigit()
True

Note that they only check for those characters, so a string with spaces or anything else will return false for both: 请注意,它们只检查这些字符,因此带有空格或其他任何内容的字符串将返回false:

>>> 'hi there'.isalpha()
False

However, if you want the value as a number, you're better off just using int . 但是,如果您希望将值作为数字,那么最好只使用int Note that there's no point checking with isinstance whether the result is indeed an integer. 请注意,使用isinstance检查结果是否确实是整数是没有意义的。 If int(blah) succeeds, it will always return an integer; 如果int(blah)成功,它将始终返回一个整数; if the string doesn't represent an integer, it will raise an exception. 如果字符串不表示整数,则会引发异常。

Take your pick. 随便挑选。

>>> '4'.isdigit()
True
>>> '-4'.isdigit()
False
>>> int('-4')
-4
>>> 'foo'.isdigit()
False
>>> int('foo')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: 'foo'

To implement the logic your asking for, I guess the most pythonic way would be to use exception handling: 为了实现你所要求的逻辑,我想最pythonic的方法是使用异常处理:

try:
  n = int(random_string)
except ValueError: 
  if len(random_string) > 1:
    # it's a word
  else:
    # it's a character (or the empty string)

To check the case, you can use the string method islower() . 要检查大小写,可以使用字符串方法islower()

To gain more control, esp in case if you have some type of strings which inbuilt functions don't support, you can use re - 为了获得更多的控制,尤其是如果你有一些内置函数不支持的字符串,你可以使用re -

A regular exp like below to check if its a number - 像下面的常规exp来检查它是否是一个数字 -

re.findall(r'^[-]?[0-9]*[\.]?[0-9]*$', s)

and the following regular exp to check if its a string - 和以下常规exp来检查它是否是一个字符串 -

r'^[a-zA-Z]+$'

Please note that this is just for demo, you should modify the regular exp as per your needs. 请注意,这仅用于演示,您应该根据需要修改常规exp。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM