前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >专栏 >Python从字符串中删除字符

Python从字符串中删除字符

作者头像
全栈程序员站长
发布2022-09-01 15:05:24
发布2022-09-01 15:05:24
5.8K00
代码可运行
举报
运行总次数:0
代码可运行

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

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中是不可变的,因此此函数将返回一个新字符串,而原始字符串将保持不变。

代码语言:javascript
代码运行次数:0
运行
复制
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代码点。

代码语言:javascript
代码运行次数:0
运行
复制
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”。

代码语言:javascript
代码运行次数:0
运行
复制
s = 'abc12321cba'

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

Output: 12321

输出: 12321

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

代码语言:javascript
代码运行次数:0
运行
复制
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)

代码语言:javascript
代码运行次数:0
运行
复制
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()函数参数是字符串。 让我们看看如何从字符串中删除单词。

代码语言:javascript
代码运行次数:0
运行
复制
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()函数中传递第三个参数,以指定应该执行替换的次数。

代码语言:javascript
代码运行次数:0
运行
复制
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.cn/142002.html原文链接:https://javaforall.cn

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022年5月2,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Python从字符串中删除字符 (Python Remove Character from String)
    • Python使用replace()从字符串中删除字符 (Python Remove Character from String using replace())
    • Python使用translate()从字符串中删除字符 (Python Remove Character from String using translate())
  • 从字符串中删除空格 (Removing Spaces from a String)
  • Python从字符串中删除换行符 (Python Remove newline from String)
  • 从字符串中删除子字符串 (Remove substring from string)
  • 删除指定的次数 (Remove specified number of times)
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档