如何用下划线替换空格,反之亦然? [英] How do I replace whitespaces with underscore and vice versa?

查看:402
本文介绍了如何用下划线替换空格,反之亦然?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用字符串中的下划线替换空格来创建漂亮的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" 

这可以使用正则表达式来解决吗?

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆