{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# 转义字符"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"反斜杠(\\)是一个特殊字符,在字符串中表示转义。 \n",
"该字符与后面相邻的一个字符共同组成了一个特定的含义。 \n",
"在格式化输出字符串时,可以用转义字符实现一些特殊的格式控制。"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"| | 描述 | 示例 |\n",
"| :---- | :---- | :---- |\n",
"| \\\\n | 换行 | n:newline,用于行末,表示输出时到当前位置本行结束,后面字符在新的一行输出 |\n",
"| \\\\r | 回车 | r:return,回车,Mac os下表示换下一行 |\n",
"| \\\\t | 水平制表符 | 功能与键盘上tab键相同,光标水平移动若干个字符,一般3个字符,也有解析成4或6个字符 |\n",
"| \\\\(在行尾时) | 续行符 | 为避免一行太长,排版时在前一行末尾加“\\\\”,解释器会将下一行内容接在前行末尾。 |\n",
"| \\\\\\\\ | 反斜杠符号 | 用于在字符串中输出一个反斜杠“\\\\” |\n",
"| \\\\' | 单引号 | 用于在字符串中输出一个单引号 |\n",
"| \\\\\" | 双引号 | 用于在字符串中输出一个双引号 |\n",
"| \\\\b | 退格(Backspace) | 使光标回退一格,清除前面一个字符 |\n"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\t静夜思\n",
"\t 李白\n",
"床前明月光,疑是地上霜。\n",
"举头望明月,低头思故乡。\n",
"\n"
]
}
],
"source": [
"print('\\t静夜思\\n\\t 李白\\n床前明月光,疑是地上霜。\\n举头望明月,低头思故乡。\\n')"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
" 静夜思\n",
" 李白\n",
"床前明月光,\n",
"疑是地上霜。\n",
"举头望明月,\n",
"低头思故乡。\n",
"\n"
]
}
],
"source": [
"print(' 静夜思\\n 李白\\n床前明月光,\\n疑是地上霜。\\n举头望明月,\\n低头思故乡。\\n')"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.5"
}
},
"nbformat": 4,
"nbformat_minor": 4
}