如何用下划线替换空格? [英] How do I replace whitespaces with underscore?
本文介绍了如何用下划线替换空格?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想用字符串中的下划线替换空格以创建漂亮的URL。这样,例如:
I want to replace whitespace with underscore in a string to create nice URLs. So that for example:
"This should be connected" becomes "This_should_be_connected"
我在Django中使用Python。可以使用正则表达式解决吗?
I am using Python with Django. Can this be solved using regular expressions?
推荐答案
您不需要正则表达式。 Python具有满足您需要的内置字符串方法:
You don't need regular expressions. Python has a built-in string method that does what you need:
mystring.replace(" ", "_")
这篇关于如何用下划线替换空格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文