str.index(sub, start=None, end=None)

作用:查看sub是否在字符串中,在的话返回索引,且只返回第一次匹配到的索引;若找不到则报错;可以指定统计的范围,[start,end) 左闭区间右开区间

  1. str = "helloworldhhh"

  2. print(str.index("h"))
  3. print(str.index("hhh"))
  4. # print(str.index("test")) 直接报语法错误:ValueError: substring not found

执行结果

  1. 0
  2. 10

str.find(sub, start=None, end=None)

作用:和index()一样,只是找不到不会报错,而是返回-1

  1. str = "helloworldhhh"

  2. print(str.index("h"))
  3. print(str.index("hhh"))
  4. print(str.index("test"))

执行结果

  1. 0
  2. 10
  3. -1

str.count( sub, start=None, end=None)

作用:统计子字符串的数量;可以指定统计的范围,[start,end) 左闭区间右开区间

  1. str = "hello world !!! hhh"
  2.  
  3. print(str.count(" "))
  4. print(str.count(" ", 5, 10))

执行结果

  1. 3
  2. 1

str.split(str="", num=string.count(str))

作用:将字符串按照str分割成列表,如果参数 num 有指定值,则分隔 num+1 个子字符串

  1. str = "hello world !!! hhh"
  2.  
  3. print(str.split(" "))
  4. print(str.split(" ", 1))

执行结果

  1. ['hello', 'world', '!!!', 'hhh']
  2. ['hello', 'world !!! hhh']

str.strip(chars = " ")

作用:移除字符串头尾指定的字符序列chars,默认为空格

str.lstrip(chars = " ")

作用:移除字符串头部指定的字符序列chars,默认为空格

str.rstrip(chars = " ")

作用:移除字符串尾部指定的字符序列chars,默认为空格

  1. str = " hello every "
  2.  
  3. print("1", str.strip(), "1")
  4. print(str.lstrip(), "1")
  5. print("1", str.rstrip())
  6.  
  7. str = "!!! cool !!!"
  8.  
  9. print(str.strip("!"))

执行结果

  1. 1 hello every 1
  2. hello every 1
  3. 1 hello every
  4. cool

str.replace(old,new,count= -1)

作用:把字符串中的 old(旧字符串) 替换成 new(新字符串),count代表最多替换多少次,默认-1代表全部替换

  1. str = "hello world !!! hhh"
  2.  
  3. print(str.replace(" ", "-"))
  4. print(str.replace(" ", "-", 1))

执行结果

  1. hello-world-!!!-hhh
  2. hello-world !!! hhh

str.join(sequence)

作用:将序列中的元素以指定的字符连接生成一个新的字符串

  1. lists = ["1", "2", "3"]
  2. tuples = ("1", "2", "3")
  3.  
  4. print("".join(lists))
  5. print("".join(tuples))
  6. print("-".join(lists))

执行结果

  1. 123
    123
  2. 1-2-3

知识点

  • "".join(lists) 这是最常见的将列表、元组转成字符串的写法
  • 列表里面只能存放字符串元素,有其他类型的元素会报错
  • 元组也能传进去

str.upper()

作用:将字符串都变成大写字母

str.lower()

作用:将字符串都变成小写字母

  1. str = "hello world !!! hhh"
  2.  
  3. print(str.upper())
  4. print(str.lower())

执行结果

  1. HELLO WORLD !!! HHH
  2. hello world !!! hhh

str.startswith(prefix, start=None, end=None)

作用:检查字符串是否是以指定子字符串开头,如果是则返回 True,否则返回 False;可以指定统计的范围,[start,end) 左闭区间右开区间

str.endswith(self, suffix, start=None, end=None)

作用:相反这是结尾

  1. str = "hello world !!! hhh"
  2.  
  3. print(str.startswith("h"))
  4. print(str.startswith("hh"))
  5. print(str.endswith("h"))
  6. print(str.endswith("hhhh"))

执行结果

  1. True
  2. False
  3. True
  4. False

str.isdigit()

作用:检查字符串是否只由数字组成

  1. str = "123134123"
  2.  
  3. print(str.isdigit())

执行结果

  1. true

str.isalpha()

作用:检查字符串是否只由字母组成

  1. str = "abc"
  2.  
  3. print(str.isalpha())

执行结果

  1. true

str.splitlines([keepends])

作用:将字符串按照行 ('\r', '\r\n', \n') 分隔

  1. str = """
  2. 123
  3. 456
  4. 789
  5. """
  6.  
  7. print(str.splitlines())
  8.  
  9. with open("./file1.txt", encoding="utf-8") as f:
  10. lists = f.read().splitlines()
  11. print(lists)

执行结果

  1. ['', '123', '456', '789']
  2. ['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. 彻底解决Could not transfer artifact org.apache.maven.plugins问题

    今天在学习maven框架的时候出现Could not transfer artifact org.apache.maven.plugins问题,后面根据很多博客综合总结,终于解决了,现在分享一下我的方 ...

  2. [leetcode] 38. 报数(Java)(字符串处理)

    38. 报数 水题 class Solution { public String next(String num) { String ans = ""; int i = 0; wh ...

  3. 使用Relay部署编译ONNX模型

    使用Relay部署编译ONNX模型 本文介绍如何使用Relay部署ONNX模型的入门. 首先,必须安装ONNX软件包. 一个快速的解决方案是安装protobuf编译器,然后 pip install o ...

  4. Hashing散列注意事项

    Hashing散列注意事项 Numba支持内置功能hash(),只需__hash__()在提供的参数上调用成员函数即可 .这使得添加对新类型的哈希支持变得微不足道,这是因为扩展APIoverload_ ...

  5. jvm调优神器——arthas

    在上一篇<jvm调优的几种场景>中介绍了几种常见的jvm方面调优的场景,用的都是jdk自带的小工具,比如jps.jmap.jstack等.用这些自带的工具排查问题时最大的痛点就是过程比较麻 ...

  6. java8 函数式编程接口

    java8 函数式接口java.util.function.* @param T 入参类型 @param R 出参类型 1. Function <T,R> 例: Function<I ...

  7. JUC下工具类CountDownLatch用法以及源码理解

    CountDownLoatch是JUC下一个用于控制计数的计数器,比如我需要从6开始计数,每个线成运行完之后计数减一,等计数器到0时候开始执行其他任务. public static void main ...

  8. Django(65)jwt认证原理

    前言 带着问题学习是最有目的性的,我们先提出以下几个问题,看看通过这篇博客的讲解,能解决问题吗? 什么是JWT? 为什么要用JWT?它有什么优势? JWT的认证流程是怎样的? JWT的工作原理? 我们 ...

  9. [Azure DevOps] 如何使用任务组

    1. 使用 PowerShell 脚本 在上一篇文章中我们学会了怎么使用扩展在编译前实时更改版本号.有些情况下我们希望不适用扩展,例如喜欢发明轮子,或者根本没有安装扩展的权限.这时候我们可以自己写 P ...

  10. 乘风破浪,遇见下一代操作系统Windows 11,迄今为止最美版本,原生支持安卓应用

    遇见下一代操作系统Windows 11 全新Windows体验,让您与热爱的人和事物离得更近. Windows一直是世界创新的舞台.它是全球企业的基石,助力众多蓬勃发展的初创公司变得家喻户晓.网络在W ...