Python - 替换小写字母 [英] Python - replacing lower case letters

查看:65
本文介绍了Python - 替换小写字母的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<预><代码>>>>导入字符串>>>word = "你好.">>>word2 = word.replace(string.lowercase, '.')>>>打印 word2你好.

我只想把所有的小写字母都变成句号.

我在这里做错了什么?

解决方案

使用正则表达式:

from re import sub打印子([a-z]",.",你好.")

str.replace 正在寻找将字符串 abcdefghijklmnopqrstuvwxyz 替换为 .,而不是查找要替换的每个字母.

>>> import string
>>> word = "hello."
>>> word2 = word.replace(string.lowercase, '.')
>>> print word2
hello.

I just want all the lowercase letters to turn into full stops.

What am I doing wrong here?

解决方案

Use a regular expression:

from re import sub

print sub("[a-z]", '.', "hello.")

str.replace is looking for the string abcdefghijklmnopqrstuvwxyz to replace it with ., not looking for each individual letter to replace.

这篇关于Python - 替换小写字母的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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