|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593 |
- {
- "cells": [
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "# Print 函数"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "`print` 是Python内置的一个函数,可以用下列不同的方式使用。\n",
- "\n",
- " - print(\"Hello World\")\n",
- " - print(\"Hello\", <Variable Containing the String>)\n",
- " - print(\"Hello\" + <Variable Containing the String>)\n",
- " - print(\"Hello %s\" % <variable containing the string>)"
- ]
- },
- {
- "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
- }
|