字符串相关函数

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字符串相关函数的更多相关文章

  1. python 字符串 - python基础入门(12)

    在 python变量 文章中我们对python变量做了一个简单的了解,整数/浮点数/bool值相对来讲都比较简单,今天详细在讲解一下关于字符串的内容,字符串俗称:str. 在本文会大量的使用print ...

  2. 关于python字符串连接的操作

    python字符串连接的N种方式 注:本文转自http://www.cnblogs.com/dream397/p/3925436.html 这是一篇不错的文章 故转 python中有很多字符串连接方式 ...

  3. 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 ...

  4. Python 字符串

    Python访问字符串中的值 Python不支持单字符类型,单字符也在Python也是作为一个字符串使用. Python访问子字符串,可以使用方括号来截取字符串,如下实例: #!/usr/bin/py ...

  5. python字符串方法的简单使用

    学习python字符串方法的使用,对书中列举的每种方法都做一个试用,将结果记录,方便以后查询. (1) s.capitalize() ;功能:返回字符串的的副本,并将首字母大写.使用如下: >& ...

  6. python字符串基础知识

    1.python字符串可以用"aaa",'aaa',"""aaa""这三种方式来表示 2.python中的转义字符串为" ...

  7. Python 字符串格式化

    Python 字符串格式化 Python的字符串格式化有两种方式: 百分号方式.format方式 百分号的方式相对来说比较老,而format方式则是比较先进的方式,企图替换古老的方式,目前两者并存 一 ...

  8. Python 字符串操作

    Python 字符串操作(string替换.删除.截取.复制.连接.比较.查找.包含.大小写转换.分割等) 去空格及特殊符号 s.strip() .lstrip() .rstrip(',') 复制字符 ...

  9. 【C++实现python字符串函数库】strip、lstrip、rstrip方法

    [C++实现python字符串函数库]strip.lstrip.rstrip方法 这三个方法用于删除字符串首尾处指定的字符,默认删除空白符(包括'\n', '\r', '\t', ' '). s.st ...

随机推荐

  1. 我成为 Microsofti Azure MVP 啦!(ps:不是美国职业篮球)

    一,引言 今天是个高兴的日子,早上10点左右收到了来自微软的MVP的礼包的快递,对我来说,这是一件很值得纪念的日子.所以今天就水一篇心得感想吧!!! 我是年前2月9号开始申请MVP的,经历了差不多1个 ...

  2. OO_Unit3_Summary

    JML这一单元是真的有含金量,很有难度.而且这难点和前两单元完全不同,前两单元是容易架构混乱导致细节出问题,JML单元是读不懂JML规格的话架构都构不出来,以及即使能够读懂JML规格了,让自己写规格的 ...

  3. BUAA_2021_SE_Case_Analysis

    案例分析作业 项目 内容 这个作业属于哪个课程 2021春季计算机学院软件工程(罗杰 任健) 这个作业的要求在哪里 案例分析作业 我在这个课程的目标是 通过课程学习,完成第一个可以称之为"软 ...

  4. 记一次在virtualbox中安装windows7遇到增强功能安装分辨率的问题

    在windows7中遇到很多次安装好增强功能后并不能对其实现放大分辨率,使其跟随虚拟机界面大小而改变. 经过许多次的安装和恢复快照,发现了在设置中,显存的大小一直都是处于边缘的状态, 关闭虚拟机后,我 ...

  5. Mybatis3源码笔记(一)环境搭建

    1. 源码下载 地址:https://github.com/mybatis/mybatis-3.git. 国内访问有时确实有点慢,像我就直接先fork.然后从git上同步到国内的gitte上,然后在i ...

  6. Java 高精度浮点数计算工具

    说起编程中的高精度数值,我第一反应就是double类型了.的确,double阶码11位,尾数52位,几乎能应对任何苛刻的要求......然而,当我天真地尝试用double来算泰勒展开式的函数值,离散代 ...

  7. w1R3s靶机work_through

    前言 这靶机挺简单的.虽然网友们有分享oscp向的靶机集,但是没有一个难度梯度,做起来就怪怪的. 打点 nmap -sP 192.168.218.0/24 发现主机IP 192.168.218.134 ...

  8. .Net之配置文件

    1. 说明 默认情况下读取配置Configuration的默认优先级:ConfigureAppConfiguration(自定义读取)>CommandLine(命令行参数)>Environ ...

  9. MYSQL中TIMESTAMP类型的默认值理解

    MYSQL中TIMESTAMP类型可以设定默认值,就像其他类型一样. 1.自动UPDATE 和INSERT 到当前的时间:表:----------- Table   Create Table      ...

  10. hdu1316 大数

    题意:      给你一个区间,问这个区间有多少个斐波那契数. 思路:      水的大数,可以直接模拟,要是懒可以用JAVA,我模拟的,打表打到1000个就足够用了... #include<s ...