在Python中,可以使用字符串的replace()
函数来替换字符串中的空格。replace()
函数接受两个参数,第一个参数是要被替换的字符串,第二个参数是替换后的字符串。
以下是一个示例代码:
string = "Hello World"
new_string = string.replace(" ", "-")
print(new_string)
输出结果为:
Hello-World
在上面的代码中,我们使用replace()
函数将字符串中的空格替换为-
。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
相关推荐:python中怎么替换字符串