python中字符串常用的函数

s ="hello world"

s[2]  >>> "l"    索引 

s.index("l") >>> 2    索引值  (返回第一个)

s[0:3]  >>> "hel"    切片   (冒号左侧索引值开始,右侧索引值前一位结束,-1 代表最后一位)

id(s)  >>> 34733680    内存地址编号

len(s) >>> 11    字符串的长度

"h" in s >>> True    判断字符是否在字符串中

max(s) >>> "w"    返回字符串中的最大值(字母返回索引最大的)

min(s) >>> " "     返回字符串中的最小值(字母返回索引最小的)

ord("!") >>> 33    返回字符utf-8 中的十进制位置

chr(33) >>> "!"    返回当前位置的字符

"I like %s %s" %("python","!")  >>>I like python !    格式化输出 (%s 代表着占位符,用%后面()中内容代替  %s代表字符,%d 代表整数,%f 代表浮点数)

dir(s) >>> ....    返回字符串常用的方法

s.isalpha() >>> False    判断字符是否全是字母如果全是返回True

s.split(" ") >>> ['hello', 'world']    通过什么字符将字符分割成列表

s.strip() >>>"hello world"     去掉字符的左右空格

s.upper() >>> "HELLO WORLD"     字符变大写

s.isupper() >>>False     字符是否全大写

s.lower() >>> "hello world"    字符变小写

s.islower() >>> True   字符是否全小写

s.title() >>> "Hello World"     首字母变大写

s.istitle() >>> False    

s.capitalize() >>> "Hello world"    字符首字母大写 (capitalize) 

增加:

s.replace(“旧字符”,"新的字符")    #把旧的字符替换成新的字符

s.isdigit()    # 判断字符串是不是数字

原文地址:https://www.cnblogs.com/tianhen/p/8809359.html