[781]python去除字符串中开头|结尾|所有字母、数字

string是一个字符串常量的集合的包。
公共模块变量:

whitespace – 包含所有空白的字符串
ascii_lowercase – 包含所有小写字母的字符串
ascii_uppercase – 一个包含所有ASCII大写字母的字符串
ascii_letters – 包含所有ASCII字母的字符串
digits – 包含所有十进制位数的字符串
hexdigits – 包含所有 十六进制数字的字符串
octdigits – 包含所有八进制数字的字符串
punctuation – 包含所有标点字符的字符串
printable – 包含所有可打印的字符的字符串

py3

import string   # 导入string这个模块
print(string.digits)  # 输出包含数字0~9的字符串
print(string.ascii_letters)  # 包含所有字母(大写或小写)的字符串
print(string.ascii_lowercase)  # 包含所有小写字母的字符串
print(string.ascii_uppercase)  # 包含所有大写字母的字符串
##############
0123456789
abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ
abcdefghijklmnopqrstuvwxyz
ABCDEFGHIJKLMNOPQRSTUVWXYZ
print([chr(i) for i in range(65, 91)])  # 所有大写字母
print([chr(i) for i in range(97, 123)])  # 所有小写字母
print([chr(i) for i in range(48, 58)])   # 所有数字

####################
['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']
['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']

列表生成式

a = [str(x) for x in range(10)]
######################
['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']

生成随机验证码

import random
def get_code():
    source = list('0123456789')
    for i in range(97, 123):
        source.append(chr(i))
    print(''.join(random.sample(source, 4)))
def v_code():
    code = ''
    for i in range(5):
        add = random.choice([random.randrange(10), chr(random.randrange(97, 123))])
        code += str(add)
    print(code)

py2

import string   # 导入string这个模块
print(string.digits)  # 输出包含数字0~9的字符串
print(string.letters)  # 包含所有字母(大写或小写)的字符串
print(string.lowercase)  # 包含所有小写字母的字符串
print(string.uppercase)  # 包含所有大写字母的字符串
##############
0123456789
abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ
abcdefghijklmnopqrstuvwxyz
ABCDEFGHIJKLMNOPQRSTUVWXYZ

python 除去字符串开头结尾字母、数字

import string
a = 'XQX大家好'
 
print(a.strip(string.ascii_uppercase))#利用string.uppercase代表大写字母

python3除去字符串所有数字

from string import digits
 
s = 'abc123def456ghi789zero0'
remove_digits = str.maketrans('', '', digits)
res = s.translate(remove_digits)
# 'abcdefghizero'

或者:

filter(lambda x: x.isalpha(), "a1a2a3s3d4f5fg6h")

还可以:

for i in range(10):
    a.replace(str(i),'')

python2除去字符串所有数字

from string import digits
 
s = 'abc123def456ghi789zero0'
res = s.translate(None, digits)
# 'abcdefghizero'

参考:https://blog.csdn.net/qq_25792799/article/details/80322889
https://blog.csdn.net/qq_40771567/article/details/86561158
https://www.cnblogs.com/pyse/p/9847812.html
http://dy.163.com/v2/article/detail/DMA5CHFN0511RVML.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

周小董

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值