如何检查字符串是否仅包含来自python中给定集合的字符 [英] How to check if a string contains only characters from a given set in python

查看:24
本文介绍了如何检查字符串是否仅包含来自python中给定集合的字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用户输入的多项式,如果它只有字符串 1234567890^-+x 中的字符,我只想使用它.

I have a a user-inputted polynomial and I only want to use it if it only has characters in the string 1234567890^-+x.

如何在不使用外部包的情况下检查它是否有效?我只想使用内置的 Python 2.5 函数.

How can I check if it does or not without using external packages? I only want to use built-in Python 2.5 functions.

我正在编写一个无需外部软件包即可在任何 Mac 上运行的程序.

I am writing a program that runs on any Mac without needing external packages.

推荐答案

这里有一些奇怪的 ;-) 方法:

Here are some odd ;-) ways to do it:

good = set('1234567890^-+x')

if set(input_string) <= good:
    # it's good
else:
    # it's bad

if input_string.strip('1234567890^-+x'):
    # it's bad!
else:
    # it's good

这篇关于如何检查字符串是否仅包含来自python中给定集合的字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆