{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# 字符串" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 生成字符串" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Python**中可以使用一对单引号''或者双引号\"\"生成字符串。" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "hello, world\n" ] } ], "source": [ "s = \"hello, world\"\n", "print s" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "hello world\n" ] } ], "source": [ "s = 'hello world'\n", "print s" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 简单操作" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "加法:" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "'hello world'" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s = 'hello ' + 'world'\n", "s" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "字符串与数字相乘:" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "'echoechoecho'" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "\"echo\" * 3" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "字符串长度:" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "11" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "len(s)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 字符串方法" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Python**是一种面向对象的语言,面向对象的语言中一个必不可少的元素就是方法,而字符串是对象的一种,所以有很多可用的方法。\n", "\n", "跟很多语言一样,**Python**使用以下形式来调用方法:\n", "\n", " 对象.方法(参数)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 分割" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "s.split()将s按照空格(包括多个空格,制表符`\\t`,换行符`\\n`等)分割,并返回所有分割得到的字符串。" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['1', '2', '3', '4', '5']\n" ] } ], "source": [ "line = \"1 2 3 4 5\"\n", "numbers = line.split()\n", "print numbers" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "s.split(sep)以给定的sep为分隔符对s进行分割。" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['1', '2', '3', '4', '5']\n" ] } ], "source": [ "line = \"1,2,3,4,5\"\n", "numbers = line.split(',')\n", "print numbers" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 连接" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "与分割相反,s.join(str_sequence)的作用是以s为连接符将字符串序列str_sequence中的元素连接起来,并返回连接后得到的新字符串:" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "'1 2 3 4 5'" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s = ' '\n", "s.join(numbers)" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "'1,2,3,4,5'" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s = ','\n", "s.join(numbers)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 替换" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "s.replace(part1, part2)将字符串s中指定的部分part1替换成想要的部分part2,并返回新的字符串。" ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "'hello python'" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s = \"hello world\"\n", "s.replace('world', 'python')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "此时,s的值并没有变化,替换方法只是生成了一个新的字符串。" ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "'hello world'" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 大小写转换" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "s.upper()方法返回一个将s中的字母全部大写的新字符串。\n", "\n", "s.lower()方法返回一个将s中的字母全部小写的新字符串。" ] }, { "cell_type": "code", "execution_count": 12, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "'HELLO WORLD'" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "\"hello world\".upper()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "这两种方法也不会改变原来s的值:" ] }, { "cell_type": "code", "execution_count": 13, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "hello world\n", "HELLO WORLD\n" ] } ], "source": [ "s = \"HELLO WORLD\"\n", "print s.lower()\n", "print s" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 去除多余空格" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "s.strip()返回一个将s两端的多余空格除去的新字符串。\n", "\n", "s.lstrip()返回一个将s开头的多余空格除去的新字符串。\n", "\n", "s.rstrip()返回一个将s结尾的多余空格除去的新字符串。" ] }, { "cell_type": "code", "execution_count": 14, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "'hello world'" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s = \" hello world \"\n", "s.strip()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "s的值依然不会变化:" ] }, { "cell_type": "code", "execution_count": 15, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "' hello world '" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s" ] }, { "cell_type": "code", "execution_count": 16, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "'hello world '" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s.lstrip()" ] }, { "cell_type": "code", "execution_count": 17, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "' hello world'" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s.rstrip()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 更多方法" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "可以使用dir函数查看所有可以使用的方法:" ] }, { "cell_type": "code", "execution_count": 18, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "['__add__',\n", " '__class__',\n", " '__contains__',\n", " '__delattr__',\n", " '__doc__',\n", " '__eq__',\n", " '__format__',\n", " '__ge__',\n", " '__getattribute__',\n", " '__getitem__',\n", " '__getnewargs__',\n", " '__getslice__',\n", " '__gt__',\n", " '__hash__',\n", " '__init__',\n", " '__le__',\n", " '__len__',\n", " '__lt__',\n", " '__mod__',\n", " '__mul__',\n", " '__ne__',\n", " '__new__',\n", " '__reduce__',\n", " '__reduce_ex__',\n", " '__repr__',\n", " '__rmod__',\n", " '__rmul__',\n", " '__setattr__',\n", " '__sizeof__',\n", " '__str__',\n", " '__subclasshook__',\n", " '_formatter_field_name_split',\n", " '_formatter_parser',\n", " 'capitalize',\n", " 'center',\n", " 'count',\n", " 'decode',\n", " 'encode',\n", " 'endswith',\n", " 'expandtabs',\n", " 'find',\n", " 'format',\n", " 'index',\n", " 'isalnum',\n", " 'isalpha',\n", " 'isdigit',\n", " 'islower',\n", " 'isspace',\n", " 'istitle',\n", " 'isupper',\n", " 'join',\n", " 'ljust',\n", " 'lower',\n", " 'lstrip',\n", " 'partition',\n", " 'replace',\n", " 'rfind',\n", " 'rindex',\n", " 'rjust',\n", " 'rpartition',\n", " 'rsplit',\n", " 'rstrip',\n", " 'split',\n", " 'splitlines',\n", " 'startswith',\n", " 'strip',\n", " 'swapcase',\n", " 'title',\n", " 'translate',\n", " 'upper',\n", " 'zfill']" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "dir(s)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 多行字符串" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Python 用一对 `\"\"\"` 或者 `'''` 来生成多行字符串:" ] }, { "cell_type": "code", "execution_count": 19, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "hello world.\n", "it is a nice day.\n" ] } ], "source": [ "a = \"\"\"hello world.\n", "it is a nice day.\"\"\"\n", "print a" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "在储存时,我们在两行字符间加上一个换行符 `'\\n'`" ] }, { "cell_type": "code", "execution_count": 20, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "'hello world.\\nit is a nice day.'" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a" ] }, { "cell_type": "markdown", "metadata": { "collapsed": true }, "source": [ "## 使用 `()` 或者 `\\` 来换行" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "当代码太长或者为了美观起见时,我们可以使用两种方法来将一行代码转为多行代码:\n", "\n", "* ()\n", "* \\" ] }, { "cell_type": "code", "execution_count": 21, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "\"hello, world. it's a nice day. my name is xxx\"" ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = (\"hello, world. \"\n", " \"it's a nice day. \"\n", " \"my name is xxx\")\n", "a" ] }, { "cell_type": "code", "execution_count": 22, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "\"hello, world. it's a nice day. my name is xxx\"" ] }, "execution_count": 22, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = \"hello, world. \" \\\n", " \"it's a nice day. \" \\\n", " \"my name is xxx\"\n", "a" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 强制转换为字符串" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "* `str(ob)`强制将`ob`转化成字符串。 \n", "* `repr(ob)`也是强制将`ob`转化成字符串。\n", "\n", "不同点如下:" ] }, { "cell_type": "code", "execution_count": 23, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "'3.3'" ] }, "execution_count": 23, "metadata": {}, "output_type": "execute_result" } ], "source": [ "str(1.1 + 2.2)" ] }, { "cell_type": "code", "execution_count": 24, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "'3.3000000000000003'" ] }, "execution_count": 24, "metadata": {}, "output_type": "execute_result" } ], "source": [ "repr(1.1 + 2.2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 整数与不同进制的字符串的转化" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "可以将整数按照不同进制转化为不同类型的字符串。\n", "\n", "十六进制:" ] }, { "cell_type": "code", "execution_count": 25, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "'0xff'" ] }, "execution_count": 25, "metadata": {}, "output_type": "execute_result" } ], "source": [ "hex(255)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "八进制:" ] }, { "cell_type": "code", "execution_count": 26, "metadata": { "collapsed": false, "scrolled": true }, "outputs": [ { "data": { "text/plain": [ "'0377'" ] }, "execution_count": 26, "metadata": {}, "output_type": "execute_result" } ], "source": [ "oct(255)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "二进制:" ] }, { "cell_type": "code", "execution_count": 27, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "'0b11111111'" ] }, "execution_count": 27, "metadata": {}, "output_type": "execute_result" } ], "source": [ "bin(255)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "可以使用 `int` 将字符串转为整数:" ] }, { "cell_type": "code", "execution_count": 28, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "23" ] }, "execution_count": 28, "metadata": {}, "output_type": "execute_result" } ], "source": [ "int('23')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "还可以指定按照多少进制来进行转换,最后返回十进制表达的整数:" ] }, { "cell_type": "code", "execution_count": 29, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "255" ] }, "execution_count": 29, "metadata": {}, "output_type": "execute_result" } ], "source": [ "int('FF', 16)" ] }, { "cell_type": "code", "execution_count": 30, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "255" ] }, "execution_count": 30, "metadata": {}, "output_type": "execute_result" } ], "source": [ "int('377', 8)" ] }, { "cell_type": "code", "execution_count": 31, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "255" ] }, "execution_count": 31, "metadata": {}, "output_type": "execute_result" } ], "source": [ "int('11111111', 2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "`float` 可以将字符串转换为浮点数:" ] }, { "cell_type": "code", "execution_count": 32, "metadata": { "collapsed": false, "scrolled": true }, "outputs": [ { "data": { "text/plain": [ "3.5" ] }, "execution_count": 32, "metadata": {}, "output_type": "execute_result" } ], "source": [ "float('3.5')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 格式化字符串" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Python**用字符串的`format()`方法来格式化字符串。\n", "\n", "具体用法如下,字符串中花括号 `{}` 的部分会被format传入的参数替代,传入的值可以是字符串,也可以是数字或者别的对象。" ] }, { "cell_type": "code", "execution_count": 33, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "'a b c'" ] }, "execution_count": 33, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'{} {} {}'.format('a', 'b', 'c')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "可以用数字指定传入参数的相对位置:" ] }, { "cell_type": "code", "execution_count": 34, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "'c b a'" ] }, "execution_count": 34, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'{2} {1} {0}'.format('a', 'b', 'c')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "还可以指定传入参数的名称:" ] }, { "cell_type": "code", "execution_count": 35, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "'blue 10 1.5'" ] }, "execution_count": 35, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'{color} {n} {x}'.format(n=10, x=1.5, color='blue')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "可以在一起混用:" ] }, { "cell_type": "code", "execution_count": 36, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "'blue 10 1.5 foo'" ] }, "execution_count": 36, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'{color} {0} {x} {1}'.format(10, 'foo', x = 1.5, color='blue')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "可以用`{:}`指定格式:" ] }, { "cell_type": "code", "execution_count": 37, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "'foo 5 6.28'" ] }, "execution_count": 37, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from math import pi\n", "\n", "'{0:10} {1:10d} {2:10.2f}'.format('foo', 5, 2 * pi)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "具体规则与C中相同。" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "也可以使用旧式的 `%` 方法进行格式化:" ] }, { "cell_type": "code", "execution_count": 38, "metadata": { "collapsed": true }, "outputs": [], "source": [ "s = \"some numbers:\"\n", "x = 1.34\n", "y = 2\n", "# 用百分号隔开,括号括起来\n", "t = \"%s %f, %d\" % (s, x, y)" ] }, { "cell_type": "code", "execution_count": 39, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "'some numbers: 1.340000, 2'" ] }, "execution_count": 39, "metadata": {}, "output_type": "execute_result" } ], "source": [ "t" ] } ], "metadata": { "kernelspec": { "display_name": "Python 2", "language": "python", "name": "python2" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 2 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython2", "version": "2.7.6" } }, "nbformat": 4, "nbformat_minor": 0 }