本帖最后由 lxping 于 2022-12-6 17:48 编辑
在字符串中,转义字符“\”有以下用途:
1、将普通含义的字符变为特殊含义,比如“n”字符就代表一个普通字母,在前面加上转义字符后变为“\n”,这时候它就变成了一个换行符;
https://fishc.com.cn/forum.php?m ... hlight=%D7%AA%D2%E5
2、将特殊含义的字符变成普通字符,比如用两根斜杠“\\”,就是将"\"的特殊转义效果消除。
- print("\n") #打印换行
-
- print("\\n")
- \n
- print("")
- SyntaxError: unterminated string literal (detected at line 1)
- print("\")
- \
复制代码
3、续行,每一行的代码数量是有限制的,而很多时候我们所要表达内容一行并不能完整表达,这时候就需要续行符来对不同行的内容进行连接,使其作为完整的一行内容来输出
- print("hello", \
- "python", \
- "hello world")
- hello python hello world
- print("hello \
- python \
- hello world")
- hello python hello world
复制代码
4、原字符,若不希望字符串中的转义字符起作用,在字符串前加r或者R
- print(r"hello\npython")
- hello\npython
- print("hello\npython")
- hello
- python
复制代码