This question already has answers here:
Why doesn't .rstrip('\n') work?
                                
                                    (5个答案)
                                
                        
                        
                            Why can't I remove quotes using `strip('\"')`?
                                
                                    (1个答案)
                                
                        
                                去年关闭。
            
                    
我正在尝试使用\n命令从句子中删除strip()字符,但这似乎不起作用。

str1 = "Hello World \n, I\n am \nhere"
print(str1.strip())


输出量

Hello World
, I
 am
here

最佳答案

strip()仅从开头和结尾删除空格字符(换行符,制表符,空格)。要在\n之间删除,请使用replace()

>>> str1 = "Hello World \n, I\n am \nhere"
>>> print(str1.replace('\n', ''))
Hello World , I am here
>>>

关于python - 如何使用strip()从句子中删除\n字符,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/48346572/

10-14 01:11