首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在Python中移除字符串中的特定字符

在Python中移除字符串中的特定字符
EN

Stack Overflow用户
提问于 2010-10-15 11:46:21
回答 25查看 1.6M关注 0票数 650

我正在尝试使用Python从字符串中删除特定字符。这就是我现在使用的代码。不幸的是,它似乎没有对字符串做任何事情。

代码语言:javascript
复制
for char in line:
    if char in " ?.!/;:":
        line.replace(char,'')

我该如何正确地做到这一点?

EN

Stack Overflow用户

发布于 2014-05-25 17:34:51

代码语言:javascript
复制
#!/usr/bin/python
import re

strs = "how^ much for{} the maple syrup? $20.99? That's[] ricidulous!!!"
print strs
nstr = re.sub(r'[?|$|.|!|a|b]',r' ',strs)#i have taken special character to remove but any #character can be added here
print nstr
nestr = re.sub(r'[^a-zA-Z0-9 ]',r'',nstr)#for removing special character
print nestr
票数 1
EN
查看全部 25 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/3939361

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档