Python 字符串替换空格为逗号
您可以使用 Python 的 replace() 方法来替换字符串中的空格为逗号。以下是一个示例代码:
string = 'This is a sentence with spaces'
new_string = string.replace(' ', ',')
print(new_string)
输出:
This,is,a,sentence,with,spaces
在上述代码中,我们首先定义了一个字符串 string,然后使用 replace() 方法将其中的空格替换为逗号。最后,我们打印出替换后的新字符串 new_string。
原文地址: https://www.cveoy.top/t/topic/peLE 著作权归作者所有。请勿转载和采集!