Python - 字符串常用函数详解
str.index(sub, start=None, end=None)
作用:查看sub是否在字符串中,在的话返回索引,且只返回第一次匹配到的索引;若找不到则报错;可以指定统计的范围,[start,end) 左闭区间右开区间
str = "helloworldhhh"
print(str.index("h"))
print(str.index("hhh"))
# print(str.index("test")) 直接报语法错误:ValueError: substring not found
执行结果
0
10
str.find(sub, start=None, end=None)
作用:和index()一样,只是找不到不会报错,而是返回-1
str = "helloworldhhh"
print(str.index("h"))
print(str.index("hhh"))
print(str.index("test"))
执行结果
0
10
-1
str.count( sub, start=None, end=None)
作用:统计子字符串的数量;可以指定统计的范围,[start,end) 左闭区间右开区间
str = "hello world !!! hhh"
print(str.count(" "))
print(str.count(" ", 5, 10))
执行结果
3
1
str.split(str="", num=string.count(str))
作用:将字符串按照str分割成列表,如果参数 num 有指定值,则分隔 num+1 个子字符串
str = "hello world !!! hhh"
print(str.split(" "))
print(str.split(" ", 1))
执行结果
['hello', 'world', '!!!', 'hhh']
['hello', 'world !!! hhh']
str.strip(chars = " ")
作用:移除字符串头尾指定的字符序列chars,默认为空格
str.lstrip(chars = " ")
作用:移除字符串头部指定的字符序列chars,默认为空格
str.rstrip(chars = " ")
作用:移除字符串尾部指定的字符序列chars,默认为空格
str = " hello every "
print("1", str.strip(), "1")
print(str.lstrip(), "1")
print("1", str.rstrip())
str = "!!! cool !!!"
print(str.strip("!"))
执行结果
1 hello every 1
hello every 1
1 hello every
cool
str.replace(old,new,count= -1)
作用:把字符串中的 old(旧字符串) 替换成 new(新字符串),count代表最多替换多少次,默认-1代表全部替换
str = "hello world !!! hhh"
print(str.replace(" ", "-"))
print(str.replace(" ", "-", 1))
执行结果
hello-world-!!!-hhh
hello-world !!! hhh
str.join(sequence)
作用:将序列中的元素以指定的字符连接生成一个新的字符串
lists = ["1", "2", "3"]
tuples = ("1", "2", "3") print("".join(lists))
print("".join(tuples))
print("-".join(lists))
执行结果
123
123
1-2-3
知识点
- "".join(lists) 这是最常见的将列表、元组转成字符串的写法
- 列表里面只能存放字符串元素,有其他类型的元素会报错
- 元组也能传进去
str.upper()
作用:将字符串都变成大写字母
str.lower()
作用:将字符串都变成小写字母
str = "hello world !!! hhh" print(str.upper())
print(str.lower())
执行结果
HELLO WORLD !!! HHH
hello world !!! hhh
str.startswith(prefix, start=None, end=None)
作用:检查字符串是否是以指定子字符串开头,如果是则返回 True,否则返回 False;可以指定统计的范围,[start,end) 左闭区间右开区间
str.endswith(self, suffix, start=None, end=None)
作用:相反这是结尾
str = "hello world !!! hhh"
print(str.startswith("h"))
print(str.startswith("hh"))
print(str.endswith("h"))
print(str.endswith("hhhh"))
执行结果
True
False
True
False
str.isdigit()
作用:检查字符串是否只由数字组成
str = "123134123" print(str.isdigit())
执行结果
true
str.isalpha()
作用:检查字符串是否只由字母组成
str = "abc" print(str.isalpha())
执行结果
true
str.splitlines([keepends])
作用:将字符串按照行 ('\r', '\r\n', \n') 分隔
str = """
123
456
789
""" print(str.splitlines()) with open("./file1.txt", encoding="utf-8") as f:
lists = f.read().splitlines()
print(lists)
执行结果
['', '123', '456', '789']
['name: Jack ; salary: 12000', ' name :Mike ; salary: 12300', 'name: Luk ; salary: 10030', ' name :Tim ; salary: 9000', 'name: John ; salary: 12000', 'name: Lisa ; salary: 11000']
Python - 字符串常用函数详解的更多相关文章
- STL之map与pair与unordered_map常用函数详解
STL之map与pair与unordered_map常用函数详解 一.map的概述 map是STL的一个关联容器,它提供一对一(其中第一个可以称为关键字,每个关键字只能在map中出现一次,第二个可能称 ...
- php缓存技术——memcache常用函数详解
php缓存技术——memcache常用函数详解 2016-04-07 aileen PHP编程 Memcache函数库是在PECL(PHP Extension Community Library)中, ...
- # OpenGL常用函数详解(持续更新)
OpenGL常用函数详解(持续更新) 初始化 void glutInit(int* argc,char** argv)初始化GULT库,对应main函数的两个参数 void gultInitWindo ...
- python基础之函数详解
Python基础之函数详解 目录 Python基础之函数详解 一.函数的定义 二.函数的调用 三.函数返回值 四.函数的参数 4.1 位置参数 4.2 关键字参数 实参:位置实参和关键字参数的混合使用 ...
- Python内置函数详解
置顶 内置函数详解 https://docs.python.org/3/library/functions.html?highlight=built#ascii https://docs.pyth ...
- Python—字符串常用函数
Python-字符串常用字符串 字符串是一种表示文本的数据类型,使用单引号和双引号及三引号表示 访问字符串中的值字符串的每个字符都对应一个下标,下标编号是从0开始 转义字符字符串的格式化输出切片常用函 ...
- python字符串 常用函数 格式化字符串 字符串替换 制表符 换行符 删除空白 国际货币格式
# 字符串常用函数# 转大写print('bmw'.upper()) # BMW# 转小写print('BMW'.lower()) # bmw# 首字母大写print('how aae you ?'. ...
- python中常用模块详解二
log模块的讲解 Python 使用logging模块记录日志涉及四个主要类,使用官方文档中的概括最为合适: logger提供了应用程序可以直接使用的接口API: handler将(logger创建的 ...
- Python 字符串常用函数
操作字符串的常用函数 函数 描述(返回值) str.capitalize() 将字符串的第一个字符大写 str.title() 返回标题化的字符串,即每个单词的首字母都大写 str.upper() 全 ...
随机推荐
- 基于 BDD 理论的 Nebula 集成测试框架重构(上篇)
本文首发于 Nebula Graph 公众号 NebulaGraphCommunity,Follow 看大厂图数据库技术实践. 测试框架的演进 截止目前为止,在 Nebula Graph 的开发过程中 ...
- gin中间件推荐
中间件推荐 1.1.1. 列表 谷歌翻译欢迎查看原文 https://github.com/gin-gonic/contrib/blob/master/README.md RestGate - RES ...
- GO学习-(23) Go语言操作MySQL + 强大的sqlx
Go语言操作MySQL MySQL是业界常用的关系型数据库,本文介绍了Go语言如何操作MySQL数据库. Go操作MySQL 连接 Go语言中的database/sql包提供了保证SQL或类SQL数据 ...
- 常用正则表达式RE(慕课网_Meshare_huang)
import re str1 = 'imooc python' # str1.find('l1') 输出: -1 # str1.find('imooc') 0 # str1.startswith('i ...
- IoU、GIoU、DIoU、CIoU损失函数
IoU.GIoU.DIoU.CIoU损失函数 目标检测任务的损失函数由Classificition Loss和Bounding Box Regeression Loss两部分构成.目标检测任务中近几年 ...
- 77GHz 和24GHz Radar性能解析
77GHz 和24GHz Radar性能解析 一.77GHz MRR 77GHz MRR Automotive Collision Warning Radar Application MRR – Fo ...
- 初具雏形的UL标准侧重于自主车辆的安全性
初具雏形的UL标准侧重于自主车辆的安全性 Nascent UL standard focuses on autonomous vehicle safety 就任何自主汽车(AV)的安全性进行可信的争论 ...
- kali2020.4中安装nessus 8.14.0
1.下载软件包 官网下载地址:https://www.tenable.com/downloads/nessus 2.安装nessus dpkg -i /root/Nessus-8.14.0-debia ...
- 浅谈lambda表达式<最通俗易懂的讲解
Java8发布已经有一段时间了,这次发布的改动比较大,很多人将这次改动与Java5的升级相提并论.Java8其中一个很重要的新特性就是lambda表达式,允许我们将行为传到函数中.想想看,在Java8 ...
- 无需会员将有道云笔记脑图转换xmind
我的烦恼 有道云笔记有脑图功能,我平时经常用到,之所以很少用到其他脑图工具,是因为我一直用有道云笔记写笔记.因此编辑脑图和查看脑图比较方便,但是需要将脑图导出的时候目前只支持图片和xmind,但是需要 ...