Discuss / Python / 我爱代码!!!

我爱代码!!!

Topic source

没有加S4是可以正常运行的。

>>> print('''n=123

... ... f=456.789

... ... s1=\'Hello, world\'

... ... s2=\'Hello,\\'Adam\''

... ... s3=rr'Hello,\"Bart"''')

n=123

... f=456.789

... s1='Hello, world'

... s2='Hello,\'Adam''

... s3=rr'Hello,"Bart"

加上S4之后就各种问题了。

>>> print('''n=123

... f=456,789

... s1=\'Hello,world\'

... s2=\'Hello,\\'Adam\\''

... s3=r'Hello,\"Bart"'

... s4=r'''Hello,\nLisa!''')

  File "<stdin>", line 6

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

               ^

SyntaxError: invalid syntax

>>> n=123

>>> f=456.789

>>> s1='Hello,world'

>>> s2='Hello,\'Adam\''

>>> s3=r'Hello,"Bart"'

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

>>> print('n=',n,\n'f=',f,\n's1=',s1,\n's2=',s2,\n's3=',s3,\n's4=',s4)

  File "<stdin>", line 1

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

                                                                     ^

SyntaxError: unexpected character after line continuation character

烦请大神路过瞧一瞧,谢谢!

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

这样就能正常运行,'\n' 是换行标识,所以需要单引号包起来,因为不包起来会被认为是错误代码;

如果用引号包起来就是字符串,但遇到了\则会被转义

乐至可知

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

我也是试了好多遍:

保存.py文件,输入

n = '123'

f = '456.789'

s1 = 'Hello,World'

s2 = 'Hello,\'Adam\''

s3 = 'Hello,\"Bart\"'

s4 = 'Hello,\n Lisa!'

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

输出如下

C:\work>python idea3.py

 n = 123

 f = 456.789

 Hello,World

 Hello,'Adam'

 Hello,"Bart"

 Hello,

 Lisa!

C:\work>

欢迎留言

乐至可知

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

s4 = r'''Hello,

Lisa'''

or

s4 = 'Hello,/n Lisa'

宫瑜YUL

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

print ('''n=123

f=456.789

s1='Hello,world'

s2='Hello,\'Adam\''

s3=r'Hello,"Bart"'

s4 = r'\'\'Hello,     #因为默认‘’‘要以结束上面的‘’’,所以这里要加转换

Lisa!'\'\' ''')

我也是刚学,花了一点时间才做出来了


  • 1

Reply