[英]Replace a substring in a string with null in python
I have a string as 我有一个字符串
a='''123
456
789'''
I want to replace 456 with something similar to a null value, so that the corresponding output is 我想将456替换为类似于null值的东西,以便相应的输出为
123
789
The objective is to eliminate the empty line that would be generated as a result of 目的是消除由于以下原因而产生的空行:
a=a.replace('456','')
You are getting empty line after doing replace because '456'
is followed by '\\n'
. 进行替换后,您会得到空行 ,因为
'456'
后跟'\\n'
。 In order to remove empty line, replace '456\\n'
with ''
as: 为了删除空行,将
'456\\n'
替换为''
:
>>> a = a.replace('456\n', '')
>>> print(a)
123
789
where initially a
is holding the multiline string mentioned in the question as: 其中最初
a
持有问题中提到的多行字符串为:
>>> a='''123
456
789'''
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.