python去掉字符串开头的零_[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


版权声明:本文为weixin_42405890原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。