扫描二维码下载沐宇APP

沐宇

微信扫码使用沐宇小程序

沐宇

python怎么替换字符串中的空格

扬州沐宇科技
2023-08-10 17:58:07
python

在Python中,可以使用字符串的replace()函数来替换字符串中的空格。replace()函数接受两个参数,第一个参数是要被替换的字符串,第二个参数是替换后的字符串。

以下是一个示例代码:

string = "Hello World"
new_string = string.replace(" ", "-")
print(new_string)

输出结果为:

Hello-World

在上面的代码中,我们使用replace()函数将字符串中的空格替换为-

扫码添加客服微信