例如:
asking="hello! what's your name?"
Run Code Online (Sandbox Code Playgroud)
我可以这样做吗?
asking.strip("!'?")
Run Code Online (Sandbox Code Playgroud)
小智 19
一个非常简单的实现是:
out = "".join(c for c in asking if c not in ('!','.',':'))
Run Code Online (Sandbox Code Playgroud)
并继续添加任何其他类型的标点符号.
一种更有效的方式
import string
stringIn = "string.with.punctuation!"
out = stringIn.translate(stringIn.maketrans("",""), string.punctuation)
Run Code Online (Sandbox Code Playgroud)
编辑:这里有关于效率和其他实现的更多讨论: 从Python中删除字符串标点符号的最佳方法
thk*_*ang 14
import string
asking = "".join(l for l in asking if l not in string.punctuation)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
111025 次 |
| 最近记录: |