相关疑难解决方法(0)

如何在python中将字符串转换为有效的变量名?

我需要将任意字符串转换为python中有效变量名称的字符串.

这是一个非常基本的例子:

s1 = 'name/with/slashes'
s2 = 'name '

def clean(s):
    s = s.replace('/','')
    s = s.strip()
    return s

print clean(s1)+'_'#the _ is there so I can see the end of the string
Run Code Online (Sandbox Code Playgroud)

这是一种非常天真的方法.我需要检查字符串是否包含无效的变量名字符并将其替换为''

什么是pythonic方式来做到这一点?

python string variables validation

18
推荐指数
3
解决办法
8594
查看次数

标签 统计

python ×1

string ×1

validation ×1

variables ×1