Python从字符串中删除字符

Python从字符串中删除字符Sometimeswewanttoremovealloccurrencesofacharacterfromastring.Therearetwocommonwaystoachievethis.有时我们想从字符串中删除所有出现的字符。有两种常见的方法可以实现此目的。Python从字符串中删除字符(PythonRemoveCharacterfr…

大家好,又见面了,我是你们的朋友全栈君。

Sometimes we want to remove all occurrences of a character from a string. There are two common ways to achieve this.

有时我们想从字符串中删除所有出现的字符。 有两种常见的方法可以实现此目的。

Python从字符串中删除字符 (Python Remove Character from String)

  1. Using string replace() function

    使用字符串replace()函数

  2. Using string translate() function

    使用字符串translate()函数

Python使用replace()从字符串中删除字符 (Python Remove Character from String using replace())

We can use string replace() function to replace a character with a new character. If we provide an empty string as the second argument, then the character will get removed from the string.

我们可以使用字符串replace()函数将一个字符替换为一个新字符。 如果我们提供一个空字符串作为第二个参数,则该字符将从字符串中删除。

Note that the string is immutable in Python, so this function will return a new string and the original string will remain unchanged.

请注意,该字符串在Python中是不可变的,因此此函数将返回一个新字符串,而原始字符串将保持不变。

s = 'abc12321cba'

print(s.replace('a', ''))

Output: bc12321cb

输出: bc12321cb

Python使用translate()从字符串中删除字符 (Python Remove Character from String using translate())

Python string translate() function replace each character in the string using the given translation table. We have to specify the Unicode code point for the character and ‘None’ as a replacement to remove it from the result string. We can use ord() function to get the Unicode code point of a character.

Python字符串translate()函数使用给定的转换表替换字符串中的每个字符。 我们必须指定字符的Unicode代码点,并用’None’替换以将其从结果字符串中删除。 我们可以使用ord()函数获取字符的Unicode代码点。

s = 'abc12321cba'

print(s.translate({ord('a'): None}))

Output: bc12321cb

输出: bc12321cb

If you want to replace multiple characters, that can be done easily using an iterator. Let’s see how to remove characters ‘a’, ‘b’ and ‘c’ from a string.

如果要替换多个字符,可以使用迭代器轻松完成。 让我们看看如何从字符串中删除字符“ a”,“ b”和“ c”。

s = 'abc12321cba'

print(s.translate({ord(i): None for i in 'abc'}))

Output: 12321

输出: 12321

从字符串中删除空格 (Removing Spaces from a String)

s = ' 1 2 3 4 '
print(s.replace(' ', ''))  # 1234
print(s.translate({ord(i): None for i in ' '}))  # 1234

Python从字符串中删除换行符 (Python Remove newline from String)

s = 'ab\ncd\nef'
print(s.replace('\n', ''))
print(s.translate({ord('\n'): None}))

从字符串中删除子字符串 (Remove substring from string)

String replace() function arguments is string. Let’s see how to remove a word from a string.

字符串replace()函数参数是字符串。 让我们看看如何从字符串中删除单词。

s = 'ab12abc34ba'
print(s.replace('ab', ''))

Output: 12c34ba

输出: 12c34ba

删除指定的次数 (Remove specified number of times)

We can also pass a third parameter in replace() function to specify the number of times replacement should be performed.

我们还可以在replace()函数中传递第三个参数,以指定应该执行替换的次数。

s = 'abababab'
print(s.replace('a', 'A', 2))

Output: AbAbabab

输出: AbAbabab

GitHub Repository.
GitHub存储库中检出完整的python脚本和更多Python示例。

翻译自: https://www.journaldev.com/23674/python-remove-character-from-string

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/142002.html原文链接:https://javaforall.net

(0)
上一篇 2022年5月24日 下午5:00
下一篇 2022年5月24日 下午5:00


相关推荐

  • D3JS常用API

    D3JS常用APID3JS 常用 API

    2026年3月18日
    3
  • API接口文档编写–易文档

    API接口文档编写–易文档前言 之前项目需要做接口文档 因此找了许多接口工具 最后决定使用易文档来操作易文档 能像 postman 一样做接口性能测试 测试完可以把测试结果保存成文档 再者可以团队多人合作共享 1 先登录 输入项目名字和项目是否共享 2 进入创建项目后 可以新建目录 文档 预览 分享 导出操作 需要会员 3 点击对接口进行测试 不过在谷歌上需要下载扩展文件 下载完直接打开谷歌扩展拉进去即可 4 输入你的接口地址和参数之类的 点击发送即可 5 点击一键生成文档 保存好了点击文档

    2026年3月26日
    3
  • 我的EJB学习历程

    我的EJB学习历程

    2021年12月14日
    48
  • lstm分类模型_模型有哪两种

    lstm分类模型_模型有哪两种介绍LSTM模型在语言模型,机器翻译等领域取得了非凡的成就,然后LSTM网络有几百万的参数以及需要数周在多GPU系统中进行训练。因此,一下介绍两种方法来缩减网络参数以及训练时间。两种方法分别为factorizedLSTM(FLSTM)以及groupLSTM(GLSTM)。FLSTMFLSTM网络主要是将大的LSTM矩阵转化为两个小的矩阵。LSTM模型简单介绍在LST…

    2025年10月18日
    5
  • 转:SIGPIPE[通俗易懂]

    转:SIGPIPE[通俗易懂]SIGPIPEsend或者writesocket遭遇SIGPIPE信号当服务器close一个连接时,若client端接着发数据。根据TCP协议的规定,会收到一个RST响应,client再往这个服务器发送数据时,系统会发出一个SIGPIPE信号给进程,告诉进程这个…

    2022年5月30日
    34
  • centos7安装vsftp配置虚拟用户「建议收藏」

    centos7安装vsftp配置虚拟用户「建议收藏」安装前的准备关闭防火墙或者开端口权限。一般是firewalld或者iptables。systemctlstopfirewalldsystemctldisablefirewalld关闭sellinux立即关闭setenforce0重启也关闭vi/etc/selinux/config修改SELINUX=disabled查看是否关闭get

    2026年3月11日
    6

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号