python 字符串内置方法实例

一.字符串方法总结:

1.查找:

    find(rfind)、index(rindex)、count

2.变换:

    capitalize、expandtabs、swapcase、title、lower、upper、strip(rstrip/lstrip)(析取)、zfill

3.分行和折行:

    join、split(rsplit)、splitlines、partion(rpartion)

4.替换和高级替换:

    replace、translate、format

5.位置:

    center、rjust(ljust)、zfill(左边0填充)

5.is测试:

    startswith(endswith)、isalnum、isalpha、isdigit、islower、isupper、isspace、istitle

二.字典方法总结:

  字典的键值只能是不可变对象。

1.获取:

    get、setdefault(获取或创建)、items、keys、values、

2.创建:

    copy(浅复制)、fromkeys、update(更新)

3.删除:

    clear、pop、popitem

 三. 列表方法总结:

 1.增加:

    append、extend、insert、、

2.删除:

    pop、remove、

3.排序VS查找VS统计:

    sort、reverse、index、count、

四.可变集合set方法总结:

1.标准操作:

    difference(差)、intersection(交)、union(并)、symmetric_difference(对称差)、isdisjoint、issubset、issuperset、

2.增加:

    add

3.更新:

     update(t) t可以是任意的可迭代类型 ,还有一些差更新difference_update,对称差symmetric_difference更新,intersection_difference交更新

4.删除:

    clear、discard、pop、remove(若无返回KeyError)

str = 'hello aaron'

strA = 'hello agu AARON'

str.capitalize()  # 字符串首字母大写。 输出 'Hello aaron'

str.title()  # 每个单词的首字母都大写。 输出 'Hello Aaron'

str.upper()  # 每个字符都大写。 输出 'HELLO AARON'

strA.lower()  # 每个字符都小写。 输出 'hello agu aaron'

strA.swapcase()  #大小写转换。 输出 'HELLO AGU aaron'

str.center(14)  #字符串居中不够14个字符的前后补空格(先后再前)。输出 ' hello aaron  '

str.ljust(12)  #左对齐不够12个字符的右补空格。 输出 'hello aaron '

str.rjust(12)  #右对齐不够12个字符的左补空格。 输出 ' hello aaron'

str.ljust(20).rstrip()  #rstrip 剥除字符串右边所有空格。 输出 'hello aaron'

str.rjust(20).lstrip()  #左剥除

str.center(20).strip()  #前后都剥

strN = '   '

strN.isspace()  #是否所有字符为空格。 输出 True

strTab = 'HELLO world'

文章出处:http://www.cnblogs.com/aaron-agu/ 只有毅力和决心才能使人真正具有价值!
原文地址:https://www.cnblogs.com/aaron-agu/p/5334964.html