Contents
2005-06-02 字符串
继续:字符串 13:字符串的构成 "abd" 和'adf' 都是字符串,转意字符和c的很相似,但多了几个 支持unicode 的如:\u{xxxx} >>> u'Hello\u0043\u0023World!' u'HelloC#World!' "abc" \ 'def' 表示的是字符串"abcdef" "abc"+"def" 表示的是两个字符串用 空白字符连接!(资料中说的) 我不是很明白,因为输出的结果都是一样的。"空白字符"指的是什么呢? 要是很常的字符串就用""" 连着的三个" """ this is a very very long string. And it contains a lot line. it is suitable to write document of function. """ 很有用可以写模块里面的文档 14:字符串的操作 ps:我猜测有很多操作是能从库或模块中找到,虽然列了很多,多练习才有意义 (a) int(str,base)用来转换字符串 从base进制到 10进制的, 如int("111",2) ,返回的是7,int("111",4),返回的是21 同理long(str,base) (b) 字符串的连接 >>> "Hello" + " " + "World" 'Hello World' (c) 首字母大写 >>> "this is test".capitalize() 'This is test' (d) 全部变大写 >>> "this is test".upper() 'THIS IS TEST' (e) 全部变小写 >>> "THIS IS test".lower() 'this is test' (f) 大小写交换 >>> "This Is Test".swapcase() 'tHIS iS tEST' (g) 获得长度 >>> len("Hello World") 11 (h) 获得子串 和list获得子list一样 (i) 判断字符串的类型 s.isalnum() 是否都是数字和数字 s.isalpha() 是否都是字母 s.isdigit() 是否都是数字 s.islower() 是否都是小写字母 s.isupper() 是否都是大写字母 s.istitle() 是否每个单词的首字母都大写了 s.isspace() 是否由 \n , \t , \r , ' ' 组成的 (j) 查找子字符串 s.find(substring,[start [,end]]) 正向 在s[start [,end]]范围内查找(可选) s.rfind(substring,[start [,end]]) 反向 在s[start [,end]]范围内查找(可选) 找不到返回 -1; s.index(substring,[start [,end]])与s.find(substring,[start [,end]])类似 找不到返回ValueError 异常 同理 s.rindex 前面四个函数都返回的是位置 s.count(substring,[start [,end]]) 返回的找到的次数 (K)合并和分解字符串 s.join(words) 和s.split(words) 注意和一般的习惯不一直,两个是不同的 参数关系,看例子就明白了 >>> "\n".join(["Hello","World","Python","Said"]) 'Hello\nWorld\nPython\nSaid' >>> print "\n".join(["Hello","World","Python","Said"]) Hello World Python Said >>> "This is a book".split(" ") ['This', 'is', 'a', 'book'] (l)格式化字符串 和c的格式很象,如 >>> "%d and %d" % (-1,2) -1 and 2 注意是 格式是 字符串 % tuple, tuple是参数表, 特别一点的是: >>> "%c and %c"%(67,68) 'C and D' >>> "%r"%({"one":1,"two":2}) "{'two': 2, 'one': 1}" 参数r是 返回expr() 的值打印出来 >>> "%s"%({"one":1,"two":2}) "{'two': 2, 'one': 1}" 参数s是 返回str()的值打印出来 关于str 和expr的不同以后再说 更特别的 >>> print "%(name)s's height is %(height)d cm" \ ... ",%(name)s's weight is %(weight)d kg" % \ ... {"name":"Charles","height":170,"weight":70} Charles's height is 170 cm,Charles's weight is 70 kg %d这种格式可以 用这个通用的公式: %[<mapping key>][<conversion flag>][<Minimum field width>][<precision>][<lenght modifier>]<conversion type> 和c 中的printf的格式很相近 要注意的是 %mapping key 可以同一个变量对应多次。 也可以用命名空间来做 如 >>> def fun(a,b): ... print "a is %(a)d,b is %(b)d" %locals() ... >>> fun(1,2) a is 1,b is 2 locals()返回的是一个dictionary 变量名字为 key 变量的值为 value 排列格式 s.ljust(width) 左对其,不足补空格,超出就是字符串本身的长度 s.rjust(width) 右对其 s.center(width) 居中 s.strip() 去除字符串头尾的空白 包括\n \r \t " " s.lstrip() 去左边空白 s.rstrip() 去右边空白 python 2.2.3 增加了可选参数 如 >>> a="abdfsdfsdfggbbbaa" >>> a.strip("ab") # 头尾有 "a"或"b"就删掉 'dfsdfsdfgg' 以上是python的string,下一篇将是tuple sequence dictionary 等,将完成Python 的数据类型 邹胖小 2005年6月2日 祝大家快乐安康 _______________________________________________ python-chinese list [email protected] http://python.cn/mailman/listinfo/python-chinese 回复转发
交流
空白字符连接
Qiangning Hong 致 python-chinese 更多选项 17:54 (19分钟前) On 6/2/05, Chao Zou <[email protected]> wrote: [snip] > "abc" \ > 'def' > 表示的是字符串"abcdef" > "abc"+"def" 表示的是两个字符串用 空白字符连接!(资料中说的) > 我不是很明白,因为输出的结果都是一样的。"空白字符"指的是什么呢? "abc" "def" 完全等同于"abcdef",编译时直接生成一个字符串对象。 "abc"+"def"是一个字符串运算表达式,运行时将两个字符串对象做+运算,生成新的的字符串"abcdef" [snip]