Discuss / Python / 交作业,为啥不能两个r''''''

交作业,为啥不能两个r''''''

Topic source

print(r'''n=123

f=456.789 

s1='Hello,world' 

s2='Hello,\'Adam\'' 

s3= r'Hello,"Bart"'

'''

"s4 = r'''Hello,\nLisa!'''"

)

首先,打印变量的值,只需要直接打印多个已被赋值的变量名即可:

print(n,f,s1,s2,s3,s4)

然后,顺着你答案的思路,指令缺少了一部分内容,你完整的思路应该是:
print(r'''
n = 123
f = 456.789
s1 = 'Hello, world'
s2 = 'Hello, \'Adam\''
s3 = r'Hello, "Bart"'
s4 = r'''Hello,
Lisa!'''
''')

即便如此,以上代码还是会报错
因为在 s4 = r''' 这个地方的 ''',在程序看来,作为一个完整的多行字符串已经结束了
而后面的 Hello,Lisa! 是一个无效的语法,所以程序读到这里会报错

最后或许只能用转义对付不转义了 233 :
print(r'''
n = 123
f = 456.789
s1 = 'Hello, world'
s2 = 'Hello, \'Adam\''
s3 = r'Hello, "Bart"'
s4 = r\'''Hello,
Lisa!\'''
''')
通过转义使所有输出内容处于多行字符串之内,虽然最后的打印结果会多两个 \
但暂时只想到这个办法,希望其他大神来解惑

噢,有道理,多谢指教!

胡菇凉H

#4 Created at ... [Delete] [Delete and Lock User]

n = 123

f = 456.789

s1 = 'Hello, world'

s2 = 'Hello, \'Adam\''

s3 = r'Hello, "Bart"'

s4 = r'''Hello,

Lisa!'''

print(n,f,s1,s2,s3,s4,sep="\n")

print('------------------------------'*4)

print("n=123\nf=456.789\ns1=Hello,world\ns2=Hello,\'Adam\'\ns3=Hello,\"Bart\"\ns4=Hello,\nLisa!")

输出结果对比:

123

456.789

Hello, world

Hello, 'Adam'

Hello, "Bart"

Hello,

Lisa!

------------------------------------------------------------------------------------------------------------------------

n=123

f=456.789

s1=Hello,world

s2=Hello,'Adam'

s3=Hello,"Bart"

s4=Hello,

Lisa!

胡菇凉H

#5 Created at ... [Delete] [Delete and Lock User]

回复大源不是太原

print(r'''

n = 123

f = 456.789

s1 = Hello, world

s2 = Hello, 'Adam'

s3 = Hello, "Bart"

s4 = Hello,

Lisa!

''')     

你已经使用r(raw)来原样输出了,在里面就不用转义字符和多余的引号了,不然就会一一输出

print('''n = 123

f = 456.789

s1 = 'hello world'

s2 = 'hello,\\'adam\\''

s3 = r'hello,"bart"

s4 = \'''hello,

lisa!\''' ''')


  • 1

Reply