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

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. selenium除错

    1.使用命令行启动selenium 下载selenium jar包 http://selenium-release.storage.googleapis.com/index.html 此处下载的是se ...

  2. Open Live writer 远程博客管理客户端

    1.  官网地址:http://openlivewriter.org/ 点击download下载:https://openlivewriter.azureedge.net/stable/Release ...

  3. sklearn的GridSearchCV例子

    class sklearn.model_selection.GridSearchCV(estimator, param_grid, scoring=None, fit_params=None, n_j ...

  4. Doris FE负载均衡配置

    0 背景概述 Doris完全兼容了mysql协议,并且Doris FE本身通过多follower选举机制选举出master,可以保证fe本身的高可用性,也可以通过加入observer fe节点来提高f ...

  5. css (具体代码看笔记本)

    参考:https://www.cnblogs.com/liwenzhou/p/7999532.html  1. CSS语法   选择器 {属性1:值1;...;}  2. CSS导入方式   1. 行 ...

  6. 微信小程序教程系列

    微信小程序教程系列 来源:       https://blog.csdn.net/michael_ouyang/article/details/56846185 相关连接:http://blog.c ...

  7. tcpdump抓sql语句

    -A -n -i any |grep --color 'system_type' -n2 -- -E..,.@.@.f........ ...Ndh-....GP..:A.............. ...

  8. python正则表达式re库(自用)

    经典例子: 1.由26个字母组成的字符串 ^[A-Za-z]+$ 2. 中国境内邮政编码 [1-9]\d{5} 3.IP地址 0-99:[1-9]?\d 100-199:1\d{2} 200-249: ...

  9. 进程队列(Queue),Pipe(管道), Manager 进行进程之间的数据传递和传输

    进程Queue,实现进程传输的队列 1.Queue from multiprocessing import Process, Queue def f(q): q.put('1') q.put('2') ...

  10. As3.0中的位图(Bitmap/BitmapData)编程

    https://blog.csdn.net/wtuetnsrmh/article/details/12577929