{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Print 函数" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "`print` 是Python内置的一个函数,可以用下列不同的方式使用。\n", "\n", " - print(\"Hello World\")\n", " - print(\"Hello\", )\n", " - print(\"Hello\" + )\n", " - print(\"Hello %s\" % )" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Hello World\n" ] } ], "source": [ "print(\"Hello World\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "在Python中,单引号、双引号和三引号用于表示字符串。\n", "大部分单引号用于声明一个字符。\n", "声明一行时使用双引号,声明段落/多行时使用三引号。" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Hey\n", "line1line2\n" ] } ], "source": [ "print('Hey')\n", "a = 'line1\\\n", "line2\\\n", "\\\n", "'\n", "print(a)" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "My name is Rajath Kumar M.P.\n", "\n", "I love Python.\n" ] } ], "source": [ "print(\"\"\"My name is Rajath Kumar M.P.\n", "\n", "I love Python.\"\"\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "字符串可以分配给变量 _string1_ 和string2,使用`print`语句时可以调用。" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "scrolled": true }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Hello World\n", "Hello World !\n" ] } ], "source": [ "string1 = 'World'\n", "print('Hello', string1)\n", "\n", "string2 = '!'\n", "print('Hello', string1, string2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "字符串连接是两个字符串的“加法”。注意,在连接时,字符串之间不会有空格。" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "HelloWorld!\n" ] } ], "source": [ "print('Hello' + string1 + string2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**%s** 用于引用包含字符串的变量。" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Hello World\n" ] } ], "source": [ "print(\"Hello %s\" % string1)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "相似地,当我们使用其他的数据类型时\n", "\n", " - %s -> string\n", " - %d -> Integer\n", " - %f -> Float\n", " - %o -> Octal\n", " - %x -> Hexadecimal\n", " - %e -> exponential\n", " \n", "这可以用于`print`函数本身内部的转换。" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Actual Number = 18\n", "Float of the number = 18.000000\n", "Octal equivalent of the number = 22\n", "Hexadecimal equivalent of the number = 12\n", "Exponential equivalent of the number = 1.800000e+01\n" ] } ], "source": [ "print(\"Actual Number = %d\" % 18)\n", "print(\"Float of the number = %f\" % 18)\n", "print(\"Octal equivalent of the number = %o\" % 18)\n", "print(\"Hexadecimal equivalent of the number = %x\" % 18)\n", "print(\"Exponential equivalent of the number = %e\" % 18)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "当引用多个变量时使用圆括号。" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Hello World !\n" ] } ], "source": [ "print(\"Hello %s %s\" % (string1,string2))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 1. 其他例子" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "下面是使用print语句的其他不同方式。" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "I want to be printed here\n" ] } ], "source": [ "print(\"I want to be printed %s\" % 'here')" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "_A_A_A_A_A_A_A_A_A_A\n" ] } ], "source": [ "print('_A'*10)" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Jan\n", "Feb\n", "Mar\n", "Apr\n", "May\n", "Jun\n", "Jul\n", "Aug\n" ] } ], "source": [ "print(\"Jan\\nFeb\\nMar\\nApr\\nMay\\nJun\\nJul\\nAug\")" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Jan\n", "Feb\n", "Mar\n", "Apr\n", "May\n", "Jun\n", "Jul\n", "Aug\n" ] } ], "source": [ "print(\"\\n\".join(\"Jan Feb Mar Apr May Jun Jul Aug\".split(\" \")))" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "I want \\n to be printed.\n" ] } ], "source": [ "print(\"I want \\\\n to be printed.\")" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "Routine:\n", "\t- Eat\n", "\t- Sleep\n", "\t- Repeat\n", "\n" ] } ], "source": [ "print(\"\"\"\n", "Routine:\n", "\\t- Eat\n", "\\t- Sleep\\n\\t- Repeat\n", "\"\"\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# 2. `PrecisionWidth`和`FieldWidth`" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Fieldwidth是整个数字的宽度,精度是向右的宽度。你可以根据需要改变这些宽度。\n", "\n", "默认的精度宽度设置为6。" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'3.121312'" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "\"%f\" % 3.121312312312" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "注意,最多返回6个小数点。要指定小数点的数目,使用'%(fieldwidth).(precisionwidth)f'。" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'3.12131'" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "\"%.5f\" % 3.121312312312" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "如果字段宽度设置超过所需,则数据将自己对齐以调整到指定的值。" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'-33.12131'" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "\"%9.5f\" % -33.121312312312" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "零填充通过在字段宽度的开始处添加一个0来完成。" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'00000000000003.12131'" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "\"%020.5f\" % 3.121312312312" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "为了正确对齐,字段宽度中的空格可以留空,以便在使用负数时保持正确的对齐。" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " 3.121312\n", "-3.121312\n" ] } ], "source": [ "print(\"% 9f\" % 3.121312312312)\n", "print(\"% 9f\" % -3.121312312312)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "通过在字段宽度的开始处添加一个+号,可以在正数的开始处返回'+'号。" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "+3.121312\n", "-3.121312\n" ] } ], "source": [ "print(\"%+9f\" % 3.121312312312)\n", "print(\"% 9f\" % -3.121312312312)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "如上所述,当提到的字段宽度大于实际字段宽度时,数据右对齐。但是可以通过在字段宽度中指定一个负符号来实现左对齐。" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'3.121 '" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "\"%-9.3f\" % 3.121312312312" ] } ], "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.6.9" } }, "nbformat": 4, "nbformat_minor": 1 }