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

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. [UE4]崩溃的原因收录

    UTool tool; 这样声明可以编译通过,但是UE4 Editor会直接崩溃. 应该改成这样: UTool* tool;

  2. python-django-ORM,常用查询方式

    介绍django model 的一些常用查询方式 首先是一些文档性的帮助 __exact 精确等于 like ‘aaa’ __iexact 精确等于 忽略大小写 ilike ‘aaa’ __conta ...

  3. python中函数的参数

    函数参数(一) 思考一个问题,如下: 现在需要定义一个函数,这个函数能够完成2个数的加法运算,并且把结果打印出来,该怎样设计?下面的代码可以吗?有什么缺陷吗? def add2num(): a = 1 ...

  4. MYSQL体系结构-来自期刊

    MySQL三层体系结构 |-----------------------------------------------------------------------------------| | ...

  5. centos6.5 64练手安装memcached,PHP调试

    思路  先安装 memcached  然后安装php的基于扩展libmemcache ,然后安装php memcache扩展包,然后把扩展添加到php.ini 1 yum安装 简单方便 yum ins ...

  6. centos上自动执行脚本执行php文件

    1 先编写执行PHP文件的脚本 vi php.sh #!/bin/sh /usr/bin/php /etc/1.php 2把php.sh添加到自动执行任务中 cd /etc/ vi crontab * ...

  7. Spring MVC 运行流程图

  8. Socket IO Web实时推送

    1服务器pom.xml引入 <!-- 服务端 --> <dependency> <groupId>com.corundumstudio.socketio</g ...

  9. PL/SQL Developer安装教程以及汉化包安装教程

    一.安装PL/SQL 1.百度下载plsql破解版软件,官网只能使用30天 2.双击plsqldev906.exe进行安装,点击 iagree 3.默认是安装在c盘,可以根据自己需要更改安装目录 4. ...

  10. keras图像预处理-ImageDataGenerator

    相关参数描述:http://keras-cn.readthedocs.io/en/latest/preprocessing/image/其中validation_split参数(官方上使用方法未描述) ...