>>> lang = "study Python" >>> lang[0] 's' >>> lang[1] 't'
>>> "study Python"[0] 's'0 1 2 3 4 5 6 7 8 9 10 11 s t u d y l p y t h o n
>>> lang.index("p") 6
>>> lang 'study Python' #在前面“切”了若干的字符之后,再看一下该字符串,还是完整的。 >>> lang[2:9] 'udy pyt'
>>> lang 'study Python' >>> b = lang[1:] # 得到从 1 号到最末尾的字符,这时最后那个需要不用写 >>> b 'tudy Python' >>> c = lang[:] # 得到所有字符 >>> c 'study Python' >>> d = lang[:10] # 得到从第一个到 10 号之前的字符 >>> d 'study pyth'
>>> e = lang[0:10] >>> e 'study pyth'
>>> lang[1:11] 'tudy pytho' >>> lang[1:] 'tudy python'
>>> lang[1:12] 'tudy python' >>> lang[1:13] 'tudy python'
>>> id(c) 3071934536L >>> id(lang) 3071934536L
>>> lang = "study python" >>> c = lang
>>> str1 + str2 'abcdabcde' >>> str1 + "-->" + str2 'abcd-->abcde'
>>> "a" in str1 True >>> "de" in str1 False >>> "de" in str2 True
>>> max(str1) 'd' >>> max(str2) 'e' >>> min(str1) 'a'
>>> cmp(str1, str2) -1
>>> ord('a') 97 >>> ord('b') 98 >>> ord(' ') 32
>>> chr(97) 'a' >>> chr(98) 'b'
>>> cmp("a","b") #a-->97, b-->98, 97 小于 98,所以 a 小于 b -1 >>> cmp("abc","aaa") 1 >>> cmp("a","a") 0
>>> cmp("ad","c") -1
>>> cmp("123","23") -1 >>> cmp(123,23) # 也可以比较整数,这时候就是整数的直接比较了。 1 “*”
>>> str1*3 'abcdabcdabcd' >>> print "-"*20 # 不用输入很多个`-` -------------------- len()
>>> a="hello" >>> len(a) 5
>>> m = len(a) # 把结果返回后赋值给一个变量 >>> m 5 >>> type(m) # 这个返回值(变量)是一个整数型 <type 'int'>
>>> "I like %s" 'I like %s'
>>> "I like %s" % "python" 'I like python' >>> "I like %s" % "Pascal" 'I like Pascal'占位符 说明 %s 字符串(采用 str()的显示) %r 字符串(采用 repr()的显示) %c 单个字符 %b 二进制整数 %d 十进制整数 %i 十进制整数 %o 八进制整数 %x 十六进制整数 %e 指数 (基底写为 e) %E 指数 (基底写为 E) %f 浮点数 %F 浮点数,与上相同 %g 指数(e) 或浮点数 (根据显示长度) %G 指数(E)或浮点数 (根据显示长度)
>>> a = "%d years" % 15 >>> print a 15 years
>>> print "Suzhou is more than %d years. %s lives in here." % (2500, "qiwsir") Suzhou is more than 2500 years. qiwsir lives in here.
>>> print "Today's temperature is %.2f" % 12.235 Today's temperature is 12.23 >>> print "Today's temperature is %+.2f" % 12.235 Today's temperature is +12.23
>>> dir(str) ['add', 'class', 'contains', 'delattr', 'doc', 'eq', 'format', 'ge', 'getattribute', 'getitem', 'getnewargs', 'getslice', 'gt', 'hash', 'init', 'le', 'len', 'lt', 'mod', 'mul', 'ne', 'new', 'reduce', 'reduce_ex', 'repr', 'rmod', 'rmul', 'setattr', 'sizeof', 'str', 'subclasshook', '_formatter_field_name_split', '_formatter_parser', 'capitalize', 'center', 'count', 'decode', 'encode', 'endswith', 'expandtabs', 'find', 'format', 'index', 'isalnum', 'isalpha', 'isdigit', 'islower', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'partition', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']
Help on method_descriptor: isalpha(...) S.isalpha() -> bool Return True if all characters in S are alphabetic and there is at least one character in S, False otherwise.
>>> "python".isalpha() # 字符串全是字母,应该返回 True True >>> "2python".isalpha() # 字符串含非字母,返回 False False
>>> a = "I LOVE PYTHON" >>> a.split(" ") ['I', 'LOVE', 'PYTHON']
>>> b = "www.itdiffer.com" >>> b.split(".") ['www', 'itdiffer', 'com']
>>> b=" hello " # 两边有空格 >>> b.strip() 'hello' >>> b ' hello '
>>> b.lstrip() # 去掉左边的空格 'hello ' >>> b.rstrip() # 去掉右边的空格 ' hello'
>>> a = "qiwsir,Python" >>> a.upper() # 将小写字母完全变成大写字母 'QIWSIR,PYTHON' >>> a # 原数据对象并没有改变 'qiwsir,Python' >>> b = a.upper() >>> b 'QIWSIR,PYTHON' >>> c = b.lower() # 将所有的小写字母变成大写字母 >>> c 'qiwsir,Python' >>> a 'qiwsir,Python' >>> a.capitalize() # 把字符串的第一个字母变成大写 'Qiwsir,Python' >>> a # 原数据对象没有改变 'qiwsir,Python' >>> b = a.capitalize() # 新建立了一个 >>> b 'Qiwsir,Python' >>> a = "qiwsir,github" # 这里的问题就是网友白羽毛指出的,非常感谢他。 >>> a.istitle() False >>> a = "QIWSIR" # 当全是大写的时候,返回 False >>> a.istitle() False >>> a = "qIWSIR" >>> a.istitle() False >>> a = "Qiwsir,github" # 如果这样,也返回 False >>> a.istitle() False >>> a = "Qiwsir" # 这样是 True >>> a.istitle() True >>> a = 'Qiwsir,Github' # 这样也是 True >>> a.istitle() True >>> a = "Qiwsir" >>> a.isupper() False >>> a.upper().isupper() True >>> a.islower() False >>> a.lower().islower() True
>>> a = "This is a Book" >>> a.istitle() False >>> b = a.title() # 这样就把所有单词的第一个字母转化为大写 >>> b 'This Is A Book' >>> b.istitle() # 判断每个单词的第一个字母是否为大写 True
>>> b 'www.itdiffer.com' >>> c = b.split(".") >>> c ['www', 'itdiffer', 'com'] >>> ".".join(c) 'www.itdiffer.com' >>> "*".join(c) 'www*itdiffer*com'
这种拼接,是不是简单呢?
以上就是分享Python中常用操作字符串的函数与方法总结的详细内容,更多请关注Gxl网其它相关文章!
查看更多关于分享Python中常用操作字符串的函数与方法总结的详细内容...
声明:本文来自网络,不代表【好得很程序员自学网】立场,转载请注明出处:http://www.haodehen.cn/did85680