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

  1. STL之map与pair与unordered_map常用函数详解

    STL之map与pair与unordered_map常用函数详解 一.map的概述 map是STL的一个关联容器,它提供一对一(其中第一个可以称为关键字,每个关键字只能在map中出现一次,第二个可能称 ...

  2. php缓存技术——memcache常用函数详解

    php缓存技术——memcache常用函数详解 2016-04-07 aileen PHP编程 Memcache函数库是在PECL(PHP Extension Community Library)中, ...

  3. # OpenGL常用函数详解(持续更新)

    OpenGL常用函数详解(持续更新) 初始化 void glutInit(int* argc,char** argv)初始化GULT库,对应main函数的两个参数 void gultInitWindo ...

  4. python基础之函数详解

    Python基础之函数详解 目录 Python基础之函数详解 一.函数的定义 二.函数的调用 三.函数返回值 四.函数的参数 4.1 位置参数 4.2 关键字参数 实参:位置实参和关键字参数的混合使用 ...

  5. Python内置函数详解

    置顶   内置函数详解 https://docs.python.org/3/library/functions.html?highlight=built#ascii https://docs.pyth ...

  6. Python—字符串常用函数

    Python-字符串常用字符串 字符串是一种表示文本的数据类型,使用单引号和双引号及三引号表示 访问字符串中的值字符串的每个字符都对应一个下标,下标编号是从0开始 转义字符字符串的格式化输出切片常用函 ...

  7. python字符串 常用函数 格式化字符串 字符串替换 制表符 换行符 删除空白 国际货币格式

    # 字符串常用函数# 转大写print('bmw'.upper()) # BMW# 转小写print('BMW'.lower()) # bmw# 首字母大写print('how aae you ?'. ...

  8. python中常用模块详解二

    log模块的讲解 Python 使用logging模块记录日志涉及四个主要类,使用官方文档中的概括最为合适: logger提供了应用程序可以直接使用的接口API: handler将(logger创建的 ...

  9. Python 字符串常用函数

    操作字符串的常用函数 函数 描述(返回值) str.capitalize() 将字符串的第一个字符大写 str.title() 返回标题化的字符串,即每个单词的首字母都大写 str.upper() 全 ...

随机推荐

  1. DLPack构建跨框架的深度学习编译器

    DLPack构建跨框架的深度学习编译器 Tensorflow,PyTorch和ApacheMxNet等深度学习框架提供了一个功能强大的工具包,可用于快速进行原型设计和部署深度学习模型.易用性通常是以碎 ...

  2. 编译器设计-RunTime运行时环境

    编译器设计-RunTime运行时环境 Compiler Design - Run-Time Environment 作为源代码的程序仅仅是文本(代码.语句等)的集合,要使其活动,它需要在目标计算机上执 ...

  3. 在OpenShift平台上验证NVIDIA DGX系统的分布式多节点自动驾驶AI训练

    在OpenShift平台上验证NVIDIA DGX系统的分布式多节点自动驾驶AI训练 自动驾驶汽车的深度神经网络(DNN)开发是一项艰巨的工作.本文验证了DGX多节点,多GPU,分布式训练在DXC机器 ...

  4. MLPerf结果证实至强® 可有效助力深度学习训练

    MLPerf结果证实至强 可有效助力深度学习训练 核心与视觉计算事业部副总裁Wei Li通过博客回顾了英特尔这几年为提升深度学习性能所做的努力. 目前根据英特尔 至强 可扩展处理器的MLPerf结果显 ...

  5. 【VBA】日期时间

    当前日期: Sub 测试() Debug.Print Date End Sub 当前时间: Sub 测试() Debug.Print Date End Sub 几月: Sub 测试() Debug.P ...

  6. 【NX二次开发】移动WCS坐标系

    说明:移动WCS坐标系 用法: #include <uf.h> #include <uf_csys.h> extern DllExport void ufusr(char *p ...

  7. Django基础之路由层

    内容概要 路由匹配 无名有名分组 反向解析 无名有名分组反向解析(难理解) 路由分发 名称空间 伪静态 内容详细 1 路由匹配 urls.py url()方法第一个参数其实是一个正则表达式 第一个参数 ...

  8. 【题解】codeforces 1B Spreadsheets

    题意翻译 人们常用的电子表格软件(比如: Excel)采用如下所述的坐标系统:第一列被标为A,第二列为B,以此类推,第26列为Z.接下来为由两个字母构成的列号: 第27列为AA,第28列为AB-在标为 ...

  9. xshell连接时报错:Could not connect to '192.168.2.125' (port 22): Connection failed.

    解决思路: 1.首先用主机ping下虚拟机IP,看是否能ping通 2.如果ping不通就看虚拟机防火墙是否开启,service iptables status [root@mysql ~]# ser ...

  10. 解析 Nebula Graph 子图设计及实践

    本文首发于 Nebula Graph 公众号 NebulaGraphCommunity,Follow 看大厂图数据库技术实践. 前言 在先前的 Query Engine 源码解析中,我们介绍了 2.0 ...