{ "cells": [ { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "值Hello world 类型\n" ] } ], "source": [ "x='Hello world'\n", "print('值'+x+' 类型'+str(type(x)))" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "he says\" No other love but you.\"\n", "he says\" I'm not waking up another morning without being able to look at you next to me.\"\n", "If I were spelling out my favorite thing in the world, it'd be spelled 'Y-O-U'.\n", "I love you not because of who you are, but because of who I am when I am with you.\n" ] } ], "source": [ "# 转义字符\n", "print(\"he says\\\" No other love but you.\\\"\")\n", "print('he says\" I\\'m not waking up another morning without being able to look at you next to me.\"')\n", "print(\"If I were spelling out my favorite thing in the world, it'd be spelled 'Y-O-U'.\")\n", "print('I love you not because of who you are, but because of who I am when I am with you.')" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "看天气预报的时候 \n", "会先看你住的地方 \n", "今天好像会变暖和呢\n", "\n" ] } ], "source": [ "# 跨行输入\n", "x= '''看天气预报的时候 \n", "会先看你住的地方 \n", "今天好像会变暖和呢\n", "'''\n", "print(x)" ] }, { "cell_type": "code", "execution_count": 24, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "65\n", "A\n", "48\n", "32\n", "10\n", "9\n", "13\n", "92\n", "\\\\\\n\n", "29579\n", "王\n" ] } ], "source": [ "print(ord('A'))\n", "print(chr(65))\n", "print(ord('0'))\n", "print(ord(' '))\n", "print(ord('\\n'))\n", "print(ord('\\t'))\n", "print(ord('\\r'))\n", "print(ord('\\\\'))\n", "# raw 字符串格式下不转义\"\\\"\n", "print(r'\\\\\\n')\n", "print (ord('王'))\n", "print (chr(29579))" ] }, { "cell_type": "code", "execution_count": 27, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "True\n", "True\n", "False\n" ] } ], "source": [ "print('a'<'b')\n", "print('abcde'<'abdce')\n", "print('abcde'<'abcd')" ] }, { "cell_type": "code", "execution_count": 38, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "5\n", "h\n", "o\n", "l\n", "h\n", "ell\n", "ell\n", "\n", "\n", "ello\n", "hell\n", "hello\n", "ello\n", "hell\n" ] } ], "source": [ "x='hello'\n", "print(len(x))\n", "print(x[0])\n", "print(x[4])\n", "# print(x[5])\n", "print(x[-3])\n", "print(x[-5])\n", "# print(x[-6])\n", "print(x[1:4])\n", "print(x[1:-1])\n", "print(x[2:2])\n", "print(x[2:1])\n", "print(x[1:])\n", "print(x[:4])\n", "print(x[:])\n", "print(x[1:100])\n", "print(x[-100:4])" ] }, { "cell_type": "code", "execution_count": 39, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "hello world\n" ] } ], "source": [ "x=\"hello\"\n", "y=\" world\"\n", "z=x+y\n", "print(z)" ] }, { "cell_type": "code", "execution_count": 40, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "hellohellohello\n" ] } ], "source": [ "print(\"hello\"*3)" ] }, { "cell_type": "code", "execution_count": 41, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "True\n" ] } ], "source": [ "print('H'in 'Hello')" ] }, { "cell_type": "code", "execution_count": 42, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "False\n" ] } ], "source": [ "print('A'in'Hello')" ] }, { "cell_type": "code", "execution_count": 43, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "True\n" ] } ], "source": [ "print('ll'in'Hello')" ] }, { "cell_type": "code", "execution_count": 44, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "False\n" ] } ], "source": [ "print('Ho'in'Hello')" ] }, { "cell_type": "code", "execution_count": 45, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "False\n" ] } ], "source": [ "print('H' not in 'Hello')" ] }, { "cell_type": "code", "execution_count": 46, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "True\n" ] } ], "source": [ "print('A' not in'Hello')" ] }, { "cell_type": "code", "execution_count": 47, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "False\n" ] } ], "source": [ "print('ll' not in'Hello')" ] }, { "cell_type": "code", "execution_count": 48, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "True\n" ] } ], "source": [ "print('Ho' not in'Hello')" ] }, { "cell_type": "code", "execution_count": 49, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 49, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'Hello'.startswith('He')" ] }, { "cell_type": "code", "execution_count": 50, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 50, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'Hello'.startswith('H')" ] }, { "cell_type": "code", "execution_count": 51, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 51, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'Hello'.startswith('o')" ] }, { "cell_type": "code", "execution_count": 52, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 52, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'Hello'.endswith('o')" ] }, { "cell_type": "code", "execution_count": 53, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 53, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'Hello'.count('l')" ] }, { "cell_type": "code", "execution_count": 54, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 54, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'Hello'.count('He')" ] }, { "cell_type": "code", "execution_count": 55, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0" ] }, "execution_count": 55, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'Hello'.count('a')" ] }, { "cell_type": "code", "execution_count": 56, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'a,b,c'" ] }, "execution_count": 56, "metadata": {}, "output_type": "execute_result" } ], "source": [ "','.join(['a','b','c'])" ] }, { "cell_type": "code", "execution_count": 57, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'abc'" ] }, "execution_count": 57, "metadata": {}, "output_type": "execute_result" } ], "source": [ "''.join(['a','b','c'])" ] }, { "cell_type": "code", "execution_count": 58, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'6乘以7等于42'" ] }, "execution_count": 58, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'6乘以7等于{}'.format('42')" ] }, { "cell_type": "code", "execution_count": 59, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'2乘以3等于6'" ] }, "execution_count": 59, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a=2\n", "b=3\n", "c=6\n", "'{0}乘以{1}等于{2}'.format(a,b,c)" ] }, { "cell_type": "code", "execution_count": 61, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'2乘以3等于6'" ] }, "execution_count": 61, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a=2\n", "b=3\n", "c=6\n", "'{first}乘以{second}等于{third}'.format(first=a,second=b,third=c)" ] }, { "cell_type": "code", "execution_count": 64, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "5.6923076923076925\n", "5.69\n" ] } ], "source": [ "x=74/13\n", "print(x)\n", "print('{:.2f}'.format(x))" ] }, { "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.0" } }, "nbformat": 4, "nbformat_minor": 2 }