温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

使用python怎么移除字符串尾部的数字

发布时间:2021-05-13 17:19:17 来源:亿速云 阅读:392 作者:Leah 栏目:开发技术

这期内容当中小编将会给大家带来有关使用python怎么移除字符串尾部的数字,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。

t.rstrip(string.digits)

这样就全部将数字移除了,顺便将string这个模块看了下文档,也有一定的收获。

>>> import string
>>> string.digits
'0123456789'
>>> string.hexdigits
'0123456789abcdefABCDEF'
>>> string.octdigits
'01234567'
>>> string.letters
'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
>>> string.lowercase
'abcdefghijklmnopqrstuvwxyz'
>>> string.uppercase
'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
>>> string.printable
'0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~ \t\n\r\x0b\x0c'
>>> string.punctuation
'!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~'
>>> string.whitespace
'\t\n\x0b\x0c\r '
>>>

同时string可以将字符串和int,float相互转化:

>>> string.atof("1.23")
1.23
>>> string.atof("1")
1.0

转换的时候还可以制定进制的转化

>>> string.atoi("20")
20
>>> string.atoi("20",base=10)
20
>>> string.atoi("20",base=16)
32
>>> string.atoi("20",base=8)
16
>>> string.atoi("20",base=2)
Traceback (most recent call last):
 File "", line 1, in <module>
 File "/usr/lib64/python2.6/string.py", line 403, in atoi
  return _int(s, base)
ValueError: invalid literal for int() with base 2: '20'
>>> string.atoi("101",base=2)
5
>>> string.atoi("101",base=6)
37

capwords(s, sep = None)以sep作为分隔符,分割字符串是s,然后将每个字符串的首字母大写

>>> string.capwords("this is a dog")
'This Is A Dog'
>>> string.capwords("this is a dog",sep=" ")
'This Is A Dog'
>>> string.capwords("this is a dog",sep="s")
'This is a dog'
>>> string.capwords("this is a dog",sep="o")
'This is a doG'
>>>

maketrans(s, r)创建一个s到r的转换列表,然后可以使用translate()方法来实现

>>> replist=string.maketrans("123","abc")
>>> replist1=string.maketrans("456","xyz")
>>> s="123456789"
>>> s.translate(replist)
'abc456789'
>>> s.translate(replist1)
'123xyz789'

python主要应用领域有哪些

1、云计算,典型应用OpenStack。2、WEB前端开发,众多大型网站均为Python开发。3.人工智能应用,基于大数据分析和深度学习而发展出来的人工智能本质上已经无法离开python。4、系统运维工程项目,自动化运维的标配就是python+Django/flask。5、金融理财分析,量化交易,金融分析。6、大数据分析。

上述就是小编为大家分享的使用python怎么移除字符串尾部的数字了,如果刚好有类似的疑惑,不妨参照上述分析进行理解。如果想知道更多相关知识,欢迎关注亿速云行业资讯频道。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI