如何去除所有整数和空格的字符串

时间:2022-02-04 16:50:02

I want to know how to strip an input of all integers and spaces. I know the .strip() function in Python can do this, but it only works for characters at the beginning/end of the string.

我想知道如何去除所有整数和空格的输入。我知道Python中的.strip()函数可以做到这一点,但它只适用于字符串开头/结尾的字符。

Here is my code:

这是我的代码:

    battery = input("Is the phone charger turned on at the plug?").lower()
        if battery == "y" or battery == "yes":
            print("Replace the phone's battery or contact the phone's manufacturer.")
            break

So if the user inputs 'ye2s', the program will get rid of the '2' and take it as 'yes'.

因此,如果用户输入'ye2s',程序将摆脱'2'并将其视为'是'。

4 个解决方案

#1


3  

You could do it like follows using isdigit() string method:

您可以使用isdigit()字符串方法执行以下操作:

battery = ''.join(c for c in battery if not c.isdigit() and not c.isspace())

#2


2  

You can also use regular expressions to do the job, note \d means any digit \s means any space:

您也可以使用正则表达式来完成这项工作,注意\ d表示任何数字\ s表示任何空格:

>>> import re
>>> input = 'ye255 s'
>>> re.sub('[\d\s]+', '', 'ye255 s')
'yes'

#3


2  

You may use translate. The last argument to str.maketrans are the characters to delete:

你可以使用翻译。 str.maketrans的最后一个参数是要删除的字符:

>>> table = str.maketrans("", "", "0123456789 ")
>>> "ye2s with spac3es".translate(table)
'yeswithspaces'

It is likely to be faster than manipulating the string as list.

它可能比将字符串作为列表操作更快。

Dealing with all unicode decimal chars

As J.F.Sebastian noted, unicode provides many more characters being considered decimal digits.

正如J.F.Sebastian所指出的,unicode提供了更多被认为是十进制数字的字符。

All digits:

所有数字:

>>> len("".join(c for c in map(chr, range(sys.maxunicode + 1)) if c.isdecimal()))
460

So to remove all possible decimal (and space) chars:

所以删除所有可能的十进制(和空格)字符:

>>> delchars = "".join(c for c in map(chr, range(sys.maxunicode + 1)) if c.isdecimal() or c.isspace())
>>> table = str.maketrans("", "", delchars)
>>> "ye2s with spac3es".translate(table)
'yeswithspaces'

#4


0  

All good answers, nothing wrong with whatever method you choose. My answer is to use .lower() so your program will be recognising "Y" "Yes" "YEs" and "YES"

所有好的答案,你选择的方法都没有错。我的答案是使用.lower(),所以你的程序将识别“Y”“是”“YEs”和“YES”

change this line:

改变这一行:

if battery == "y" or battery == "yes":

to this line:

到这一行:

if battery.lower() == "y" or battery.lower() == "yes":

or alternatively, if you only like to use .lower() once, you can do this

或者,如果您只想使用.lower()一次,则可以执行此操作

if battery.lower() in ["y", "yes"]:

HTH.

HTH。

#1


3  

You could do it like follows using isdigit() string method:

您可以使用isdigit()字符串方法执行以下操作:

battery = ''.join(c for c in battery if not c.isdigit() and not c.isspace())

#2


2  

You can also use regular expressions to do the job, note \d means any digit \s means any space:

您也可以使用正则表达式来完成这项工作,注意\ d表示任何数字\ s表示任何空格:

>>> import re
>>> input = 'ye255 s'
>>> re.sub('[\d\s]+', '', 'ye255 s')
'yes'

#3


2  

You may use translate. The last argument to str.maketrans are the characters to delete:

你可以使用翻译。 str.maketrans的最后一个参数是要删除的字符:

>>> table = str.maketrans("", "", "0123456789 ")
>>> "ye2s with spac3es".translate(table)
'yeswithspaces'

It is likely to be faster than manipulating the string as list.

它可能比将字符串作为列表操作更快。

Dealing with all unicode decimal chars

As J.F.Sebastian noted, unicode provides many more characters being considered decimal digits.

正如J.F.Sebastian所指出的,unicode提供了更多被认为是十进制数字的字符。

All digits:

所有数字:

>>> len("".join(c for c in map(chr, range(sys.maxunicode + 1)) if c.isdecimal()))
460

So to remove all possible decimal (and space) chars:

所以删除所有可能的十进制(和空格)字符:

>>> delchars = "".join(c for c in map(chr, range(sys.maxunicode + 1)) if c.isdecimal() or c.isspace())
>>> table = str.maketrans("", "", delchars)
>>> "ye2s with spac3es".translate(table)
'yeswithspaces'

#4


0  

All good answers, nothing wrong with whatever method you choose. My answer is to use .lower() so your program will be recognising "Y" "Yes" "YEs" and "YES"

所有好的答案,你选择的方法都没有错。我的答案是使用.lower(),所以你的程序将识别“Y”“是”“YEs”和“YES”

change this line:

改变这一行:

if battery == "y" or battery == "yes":

to this line:

到这一行:

if battery.lower() == "y" or battery.lower() == "yes":

or alternatively, if you only like to use .lower() once, you can do this

或者,如果您只想使用.lower()一次,则可以执行此操作

if battery.lower() in ["y", "yes"]:

HTH.

HTH。