我们用下面这个字符串来做测试

testStr = "ZhanSan"

1、__cantains__

# 判断字符串是否包含某个子串,包含则返回true,不包含则返回false
print(testStr.__contains__("a"))
print(testStr.__contains__("x"))

2、capitailize

# 字符串的首字符大写
print(testStr.capitalize())

3、center,ljust,rjust

# 填充字符串,把字符串放在中间
print(testStr.center(10,"_")) # 填充字符串,把字符串放在左边
print(testStr.ljust(14,"_"))
# ZhanSan_______ # 填充字符串,把字符串放在右边
print(testStr.rjust(14,"_"))
# _______ZhanSan

4、count

# 统计子串在字符串中出现的次数
print(testStr.count("a"))

5、endwith,startwith

# 判断字符串是否已某个子串结尾
print(testStr.endswith("z")) # 判断字符串是否已某个子串开头
print(testStr.startswith("z"))

6、find

# 在字符串中查找某个字符串,只返回第一个查到的字符串,存在返回子串的索引,不存在返回-1
print(testStr.find("a")) print(testStr.find("X"))

7、index

# 返回子串在字符串中的索引,存在返回索引,不存在则报错
print(testStr.index("a")) print(testStr.index("X"))

8、join

# 使用指定字符连接字符串
print("_".join(testStr))
# Z_h_a_n_S_a_n

9、lower、upper

# 转换字符串为小写
print(testStr.lower())
# zhansan # 转换字符串为大写
print(testStr.upper())
# ZHANSAN

10、strip、lstrip、rstrip

# 在字符串左边去掉指定的子串
print(testStr.lstrip("Z"))
# hanSan # 在字符串右边去掉指定子串
print(testStr.rstrip("n"))
# ZhanSa # 在字符串两边去掉指定子串
print(testStr.strip("x"))

11、partition

# 按照指定的子串分割字符串
print(testStr.partition("a"))
# ('Zh', 'a', 'nSan')

12、replace

# 替换字符串
print(testStr.replace("a","T",1))
# ZhTnSan print(testStr.replace("a","T",2))
# ZhTnSTn

13、split

# 分割字符串
print(testStr.split("a"))
# ['Zh', 'nS', 'n']

14、swapcase

# 大小写互换
print(testStr.swapcase())
# zHANsAN

15、tiile

temp = "zhan ni hao"

print(temp.title())
# Zhan Ni Hao

python之字符串【str】的更多相关文章

  1. 【转】Python格式化字符串str.format()

    原文地址:http://blog.xiayf.cn/2013/01/26/python-string-format/ 每次使用Python的格式字符串(string formatter),2.7及以上 ...

  2. python基础-字符串(str)类型及内置方法

    字符串-str 用途:多用于记录描述性的内容 定义方法: # 可用'','''''',"","""""" 都可以用于定义 ...

  3. python中字符串(str)的常用处理方法

    str='python String function' 生成字符串变量str='python String function' 字符串长度获取:len(str)例:print '%s length= ...

  4. python中字符串(str)常用操作总结

    # 字符串的常用操作方法 (都是形成新的字符串,与原字符串没有关系.) 1.字符串的基本操作之切片 s = 'python hello word' # 取首不取尾,取尾要+1 # 切片取出来的字符串与 ...

  5. python之字符串str操作方法

    str.upper() (全部大写) str.lower() (全部小写) str.startswith() (以什么开头) str.endswith() (以什么结尾) str.count() (统 ...

  6. python中的str.strip()的用法

    python中字符串str的strip()方法 str.strip()就是把字符串(str)的头和尾的空格,以及位于头尾的\n \t之类给删掉. 例1:str=" ABC"prin ...

  7. 【Python从入门到精通】(九)Python中字符串的各种骚操作你已经烂熟于心了么?

    您好,我是码农飞哥,感谢您阅读本文,欢迎一键三连哦. 本文将重点介绍Python字符串的各种常用方法,字符串是实际开发中经常用到的,所有熟练的掌握它的各种用法显得尤为重要. 干货满满,建议收藏,欢迎大 ...

  8. Python字符串str的方法使用

    #!usr/bin/env python# -*-coding:utf-8-*-#字符串通常用双引号或单引号来表示:'123',"abc","字符串"#str字 ...

  9. Python语言总结 4.2. 和字符串(str,unicode等)处理有关的函数

    4.2.7. 去除控制字符:removeCtlChr Python语言总结4.2. 和字符串(str,unicode等)处理有关的函数Sidebar     Prev | Up | Next4.2.7 ...

  10. 人生苦短之我用Python篇(列表list、字典dict、元组tuple、字符串str)

    列表 创建列表 sample_list = ['a',1,('a','b')] Python 列表操作 sample_list = ['a','b',0,1,3] 得到列表中的某一个值 value_s ...

随机推荐

  1. shell 1基础

    shell简介 shell是一个用C语言编写的程序,是用户使用Linux的桥梁.shell既是一种命令语言,又是一种程序设计语言. shell脚本(shell script),是一种为shell编写的 ...

  2. Process Pool实现Python的并行执行

    参考:Python3.6.2文档 Source code: Lib/concurrent/futures/thread.py and Lib/concurrent/futures/process.py ...

  3. python写个御剑

    前言: 今天师傅叫我,写个python版的御剑.然后我之前也写过 不过不怎么样,这次有新想法. 思路: 御剑:读取御剑配置文件里的所有路径,加上用户要扫描的url.进行批量检测,如果状态码为200并且 ...

  4. Nginx-ingress-controller部署

    参考官网https://kubernetes.github.io/ingress-nginx/ 部署pod:nginx-ingress-controller/nginx-default-backend ...

  5. Hive启动异常

    [root@host ~]# hivewhich: no hbase in (/root/app/apache-maven-3.5.2/bin:/usr/local/sbin:/usr/local/b ...

  6. 黑盒测试用例设计——PICT(QQ实践)

     以QQ的状态设置来做一次实践.QQ用户可以对如下的状态方面的设置.后两张图是登录后的状态的可选项和鼠标键盘无动作后将状态切换至的可选项.默认的自动回复有三种,默认的快捷回复有四种.对于自动回复和快捷 ...

  7. Webservice 返回数据集 DataSet 及Android显示数据集LiveBindings

    一.服务端 New TSoapDataModule 添加控件 TDataSetProvider,TClientDataSet,TADOQuery,TADOConnection 添加方法 functio ...

  8. 任务调度的方式:Timer、ScheduledExecutorService、spring task、quartz、XXL-JOB、Elastic-Job

    任务调度 定时任务调度:基于给定的时间点.给定的时间间隔.给定的执行次数自动执行的任务. Timer 介绍 Timer,简单无门槛,一般也没人用. Timer位于java.util包下,其内部包含且仅 ...

  9. hashlib 加密模块使用说明

    import hashlib  #hashilib 模块 m = hashlib.md5() m.update('hello 天王盖地虎'.encode(encoding = 'utf-8)) m.h ...

  10. as3 TweenMax TweenLite方法

    as3 TweenMax TweenLite方法补充(暂停.重新播放.倒序播放).现在来好好的学习一下:   TweenLite.to(mc, 1.5, {x:100}); 里面的mc指所作用的对象, ...