繁体   English   中英

如何在Python的字符串的开头和结尾添加反斜杠(\\)

[英]How to add a backlash (\) at the beginning and the end of a string in Python

我正在尝试在Python字符串的开头和结尾添加反冲符号(\\)。 使用下面列出的代码时,等号右侧的所有内容在我的开发环境(Eclipse)上变为绿色,并且出现错误消息。

mystring = "\" + mystring + "\"

使用双斜杠( '\\\\' )而不是单斜杠:

原版的:

>>> print "\hello\"
  File "<stdin>", line 1
    print "\hello\"
                  ^
SyntaxError: EOL while scanning string literal

双:

>>> print "\\hello\\"
\hello\
>>> 

参见页面https://docs.python.org/2.0/ref/strings.html

escapeseq:“ \\” +任何ASCII字符

mystring = "\\mystring\\"

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM