Python 字符串替换分号为换行符
要将字符串中的分号 (;) 替换为换行符,可以使用 Python 中的 replace() 函数。replace() 函数接受两个参数,分别是要替换的字符串和替换后的字符串。
下面是一个示例代码:
string = 'Hello;World;Python'
new_string = string.replace(';', '\n')
print(new_string)
输出结果为:
Hello
World
Python
在这个示例中,我们将字符串 'Hello;World;Python' 中的分号替换为换行符 (\n),得到了新的字符串 'Hello\nWorld\nPython'。最后将新的字符串打印出来。
请注意,replace() 函数返回一个新的字符串,原始字符串并没有改变。如果你想要在原始字符串上进行替换操作,可以将新的字符串赋值给原始字符串变量。例如:
string = string.replace(';', '\n')
原文地址: https://www.cveoy.top/t/topic/pdCs 著作权归作者所有。请勿转载和采集!