删除“\n"在python中多行字符串的行之间 [英] Removing "\n" between the line in multiline string in python
本文介绍了删除“\n"在python中多行字符串的行之间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想删除\n"来自python中的intext.我尝试了很多方法,但都没有奏效.文本假设是这样的:
I want to remove "\n" from the intext in python. I tried many ways but all didn't work. The text suppose to be something like this:
string='''# Initializing Function named main()
def main () :
str1 = None
str2 = None
age=16
str1=str(input())
str2=str(input())
print("Entered Name: {}\n".format(str1))
print("Entered Website:{}".format(str2))
# Calling the main Function
main()'''
在行 "print("Entered Name: {}\n".format(str1))" 有 \n 我需要删除而不从 muliline 中删除其他 \n细绳.谢谢!
In line "print("Entered Name: {}\n".format(str1))" there is \n which I need to remove without removing other \n from muliline string. Thanks!
推荐答案
将多行字符串声明为原始字符串,这样 \n
不会被转义,然后替换它(使用另一个原始字符串,以便它没有再次逃脱):
Declare the multiline string as a raw string so the \n
isn't escaped, then replace it (using another raw string so it is not escaped yet again):
# r before the ''' makes it a raw string
string=r'''# Initializing Function named main()
def main () :
str1 = None
str2 = None
age=16
str1=str(input())
str2=str(input())
print("Entered Name: {}\n".format(str1))
print("Entered Website:{}".format(str2))
# Calling the main Function
main()'''
string = string.replace(r'\n', '')
这篇关于删除“\n"在python中多行字符串的行之间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文