009.Python字符串相关函数
字符串相关函数
1 capitalize 字符串首字母大写
strvar = "this is a dog"
res = strvar.capitalize()
print(res)
执行
[root@node10 python]# python3 test.py
This is a dog
2 title 每个单词的首字母大写
非字母隔开的单词
strvar = "this is123a dog"
res = strvar.title()
print(res)
执行
[root@node10 python]# python3 test.py
This Is123A Dog
3 upper 将所有字母变成大写
strvar = "A C c d"
res = strvar.upper()
print(res)
执行
[root@node10 python]# python3 test.py
A C C D
4 lower 将所有字母变成小写
strvar = "A C c d"
res = strvar.lower()
print(res)
执行
[root@node10 python]# python3 test.py
a c c d
5 swapcase 大小写互换
strvar = "A C c d"
res = strvar.swapcase()
print(res)
执行
[root@node10 python]# python3 test.py
a c C D
6 count 统计字符串中某个元素的数量
strvar = "aa bbccfsdfkjak"
res = strvar.count("a")
print(res)
执行
[root@node10 python]# python3 test.py
3
7 find 查找某个字符串第一次出现的索引位置
strvar = "oh Father this is My Favarate boY"
res = strvar.find("F") # 3
print(res)
res = strvar.find("F",4) # 21
print(res)
res = strvar.find("F",10,20)
print(res)
res = strvar.find("aa")
print(res)
执行
[root@node10 python]# python3 test.py
3
21
-1
-1
#index 与 find 功能相同 find找不到返回-1,index找不到数据直接报错
# res = strvar.index("aa") # 推荐使用find
8 startswith 判断是否以某个字符或字符串为开头
strvar = "oh Father this is My Favarate boY"
res= strvar.startswith("oh")
print(res)
res = strvar.startswith("thi",10) # True
print(res)
res = strvar.startswith("thi",10,12)
print(res)
执行
[root@node10 python]# python3 test.py
True
True
False
9 endswith 判断是否以某个字符或字符串结尾
strvar = "oh Father this is My Favarate boY"
res = strvar.endswith("boY")
print(res)
res = strvar.endswith("bo",-4,-1) # bo
print(res)
执行
[root@node10 python]# python3 test.py
True
True
10 split 按某字符将字符串分割成列表(默认从左到右按空格分割)
strvar = "you can you up no can no bb"
res = strvar.split()
print(res)
strvar = "you=can=you=up=no=can=no=bb"
res = strvar.split("=",2) # 第二个参数是分割几次 (从左向右)
print(res)
# rsplit 从右向左分割
res = strvar.rsplit("=",1) # (从右向左)
print(res) # 返回列表
执行

11 join 按某字符将列表拼接成字符串(容器类型都可)
listvar = ['you', 'can',"a","basestring"]
res = "*".join(listvar)
print(res) # 返回字符串
执行
[root@node10 python]# python3 test.py
you*can*a*basestring
12 replace 替换字符串(第三个参数选择替换的次数)
strvar = "可爱的小狼狗喜欢吃肉,有没有,有没有,还有没有"
res = strvar.replace("有没有","真没有")
print(res)
# 第三个参数为替换的次数
res = strvar.replace("有没有","真没有",1)
print(res)
执行
[root@node10 python]# python3 test.py
可爱的小狼狗喜欢吃肉,真没有,真没有,还真没有
可爱的小狼狗喜欢吃肉,真没有,有没有,还有没有
13 isdecimal 检测字符串是否以数字组成 必须是纯数字
res = "11323"
print(res.isdecimal())
res = "1132....3"
print(res.isdecimal())
执行
[root@node10 python]# python3 test.py
True
False
14 len 计算容器类型长度
res = len("aabbcc")
print(res)
执行
[root@node10 python]# python3 test.py
6
15 center 填充字符串,原字符居中 (默认填充空格)
strvar = "你好"
res = strvar.center(10,"*") #center(填充的个数,填充的字符)
print(res)
执行
[root@node10 python]# python3 test.py
****你好****
16 strip 默认去掉首尾两边的空白符
strvar = "\r sdf \t \n"
print(strvar)
res = strvar.strip()
print(res)
执行
[root@node10 python]# python3 test.py
sdf sdf
009.Python字符串相关函数的更多相关文章
- python 字符串 - python基础入门(12)
在 python变量 文章中我们对python变量做了一个简单的了解,整数/浮点数/bool值相对来讲都比较简单,今天详细在讲解一下关于字符串的内容,字符串俗称:str. 在本文会大量的使用print ...
- 关于python字符串连接的操作
python字符串连接的N种方式 注:本文转自http://www.cnblogs.com/dream397/p/3925436.html 这是一篇不错的文章 故转 python中有很多字符串连接方式 ...
- StackOverFlow排错翻译 - Python字符串替换: How do I replace everything between two strings without replacing the strings?
StackOverFlow排错翻译 - Python字符串替换: How do I replace everything between two strings without replacing t ...
- Python 字符串
Python访问字符串中的值 Python不支持单字符类型,单字符也在Python也是作为一个字符串使用. Python访问子字符串,可以使用方括号来截取字符串,如下实例: #!/usr/bin/py ...
- python字符串方法的简单使用
学习python字符串方法的使用,对书中列举的每种方法都做一个试用,将结果记录,方便以后查询. (1) s.capitalize() ;功能:返回字符串的的副本,并将首字母大写.使用如下: >& ...
- python字符串基础知识
1.python字符串可以用"aaa",'aaa',"""aaa""这三种方式来表示 2.python中的转义字符串为" ...
- Python 字符串格式化
Python 字符串格式化 Python的字符串格式化有两种方式: 百分号方式.format方式 百分号的方式相对来说比较老,而format方式则是比较先进的方式,企图替换古老的方式,目前两者并存 一 ...
- Python 字符串操作
Python 字符串操作(string替换.删除.截取.复制.连接.比较.查找.包含.大小写转换.分割等) 去空格及特殊符号 s.strip() .lstrip() .rstrip(',') 复制字符 ...
- 【C++实现python字符串函数库】strip、lstrip、rstrip方法
[C++实现python字符串函数库]strip.lstrip.rstrip方法 这三个方法用于删除字符串首尾处指定的字符,默认删除空白符(包括'\n', '\r', '\t', ' '). s.st ...
随机推荐
- Java8中的默认方法
作者:汤圆 个人博客:javalover.cc 前言 大家好啊,我是汤圆,今天给大家带来的是<Java8中的默认方法>,希望对大家有帮助,谢谢 文章纯属原创,个人总结难免有差错,如果有,麻 ...
- vue 快速入门 系列 —— vue 的基础应用(下)
其他章节请看: vue 快速入门 系列 vue 的基础应用(下) 上篇聚焦于基础知识的介绍:本篇聚焦于基础知识的应用. 递归组件 组件是可以在它们自己的模板中调用自身的.不过它们只能通过 name 选 ...
- 网络编程NIO之Reactor线程模型
目录 单Reactor线程模型 基于工作线程的Reactor线程模型 多Reactor线程模型 多Reactor线程模型示例 结束语 上篇文章中写了一些NIO相关的知识以及简单的NIO实现示例,但是示 ...
- 数据结构之栈(JavaScript描述)
栈数据结构 栈是一种遵从后进先出原则的有序集合.新添加或待删除的元素都保存在栈的同一端,称为栈顶,另一端就叫栈底.在栈内,锌元素都靠近栈顶,救援都接近栈底 类似栈的例子 栈也被用在编程语言你的 ...
- Python基础语法和数据类型最全总结
摘要:总结了Python最全基础语法和数据类型总结,一文带你学会Python. 本文分享自华为云社区<Python最全基础语法和数据类型总结>,原文作者:北山啦 . 人生苦短,我用Pyth ...
- 基于MVC框架的JavaWeb网站开发demo项目(JSP+Servlet+JavaBean)
1.环境配置 Windows10+Eclipse2020+jdk8+Tomcat9+MySQL8+Navicat10 2.需求分析 ①用户登录注册注销(查找.增加) ②显示用户列表(查找) ③显示用户 ...
- Mybatis的简单增删改查
刚开始学习Mybatis可以先看下官方文档,MyBatis是支持定制化SQL.存储过程以及高级映射的优秀的持久层框架.MyBatis避免了几乎所有的JDBC代码和手工设置参数以及抽取结果集.MyBat ...
- php将IP地址转换为真实地址的方法
方法使用示例如下 define('WEB_ROOT',dirname(__FILE__)); echo convertip('111.63.244.69','full'); func converti ...
- UVA11729突击战(汇报和执行任务)
题意: 你是一个长官,有一些士兵要跟你先汇报任务后在去执行任务,每次只能接受一个人的汇报,但是每一时刻可以有多个士兵在执行任务,问所有任务执行完要的最小时间. 思路: 按执行 ...
- hdu1530 最大团简单题目
题意: 给你一个无向图,让你找到这个图里面的最大团是多少. 思路: 最大图案是NP问题,直接暴力搜索,如果当前的这个点可以加入当前最大团,那么就选择加入或者舍去,如果不能加入, ...