字符串内建函数

2023-05-16

find函数查找

strint_example = "hello world good night"
index = strint_example.find("good")
print(index)
12

index函数检索

strint_example = "hello world good night"
index = strint_example.index("good",0,10)#从零开始检索good是否包含在内
print(index)
Traceback (most recent call last):
  File "E:/pycharm202.2.3/pythonProject1/demo1.py", line 2, in <module>
    index = strint_example.index("good",0,10)
ValueError: substring not found

Process finished with exit code 1

如果包含就不报错
strint_example = "hello world good night"
index = strint_example.index("good",1,17)
print(index)
12    #包含运行的结果

count函数基数

strint_example = "hello world good night"
result = strint_example.count("good")
print(result)
1

replace函数替换

strint_example = "hello world good night"
new_string = strint_example.replace("night","evening",1)	#1代表替换次数,不能超过这个次数
print(new_string)
hello world good evening

split函数分隔符

strint_example = "hello world good night"
print(strint_example.split())
print(strint_example.split("e",1))
print(strint_example.split("h"))
['hello', 'world', 'good', 'night']
['h', 'llo world good night']
['', 'ello world good nig', 't']

capitalize首字母大写其他小写

old_string = "hello World Good Night"
new_string = old_string.capitalize()
print(new_string)
Hello world good night

title函数标题化(所有单词都是大写开始,其余均为小写)

old_string = "hello World Good Night"
new_string = old_string.title()
print(new_string)
Hello World Good Night

startwith(检索字符窜是否是指定字符串开头)

old_string = "hello World Good Night"
new_string = old_string.startswith("hello")
print(new_string)
True

endwith检索结尾

old_string = "hello World Good Night"
new_string_one = old_string.endswith("Night")
new_string_tow = old_string.endswith("night")
print(new_string_one)
print(new_string_tow)
True
False

upper函数

old_string = "hello World Good Night"
new_string = old_string.upper()
print(new_string)
HELLO WORLD GOOD NIGHT

ljust(rjust)函数(字符串左对齐)

old_string = "hello World Good Night"
new_string = old_string.ljust(30,'-')	#  宽度为30,单引号里面为剩余位置的情况
print(new_string)
hello World Good Night--------

old_string = "hello World Good Night"
new_string = old_string.rjust(30,'-')
print(new_string)
--------hello World Good Night

lstrip(rstrip)截掉左/右边空格或者指定字符

old_string_one = "     hello World Good Night"
old_string_tow = "hello World Good Night     "
new_string_one = old_string_one.lstrip()
new_string_tow = old_string_tow.rstrip()
print(new_string_one)
print(new_string_tow)
hello World Good Night
hello World Good Night
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

字符串内建函数 的相关文章

  • debian10 简单搭建squid

    安装 span class token function apt get span span class token function install span squid 添加密码文件 此文件储存squid的用户与密码 span clas
  • 用python对远程服务器文件进行压缩

    前段时间 xff0c 学校有一个运维的实训 xff0c 老师布置了一个作业 用python对centos下的 home qzm bash history文件进行压缩 xff0c 我就在网上搜了一下 xff0c 发现有些我不会用 xff0c
  • CSP模测——咕咕东想吃饭

    咕咕东想吃饭 一 题目 咕咕东考试周开始了 xff0c 考试周一共有n天 他不想考试周这么累 xff0c 于是打算每天都吃顿好的 他决定每天都吃生煎 xff0c 咕咕东每天需要买ai个生煎 但是生煎店为了刺激消费 xff0c 只有两种购买方
  • Ubuntu 14.04 Storm(单机版)安装

    以下是本人自己成功安装的详细步骤 xff0c 希望对大家有所帮助 xff1a 一 安装JDK 1 从官网下载最新版本的JDK 2 将JDK解压到指定的文件夹中 xff1a sudo mkdir usr lib jvm tar span cl
  • 第十二周模测——消消乐大师——Q老师

    消消乐大师 Q老师 一 题目 Q老师是个很老实的老师 xff0c 最近在积极准备考研 Q老师平时只喜欢用Linux系统 xff0c 所以Q老师的电 脑上没什么娱乐的游戏 xff0c 所以Q老师平时除了玩Linux上的赛车游戏SuperTux
  • 第十二周作业——三维空间逃生

    三维空间逃生 一 题目 zjm被困在一个三维的空间中 现在要寻找最短路径逃生 xff01 空间由立方体单位构成 zjm每次向上下前后左右移动一个单位需要一分钟 xff0c 且zjm不能对角线移动空间的四周封闭 zjm的目标是走到空间的出口
  • Ceph — 使用cephadm搭建Ceph集群

    文章目录 准备安装 cephadm部署集群 本文将通过cephadm工具来学习如何简单地搭建一个octopus版集群 准备 服务器 主机名iposcpu 内存数据盘mgr 01192 168 2 15Centos7 72C4G无node 0
  • 国王的游戏(贪心,高精度)

    链接 xff1a https ac nowcoder com acm problem 16561 来源 xff1a 牛客网 题目描述 恰逢 H 国国庆 xff0c 国王邀请 n 位大臣来玩一个有奖游戏 首先 xff0c 他让每个大臣在左 右
  • Python下载安装you-get及使用指令

    一 安装 按WIN 43 R打开Windows的运行窗口 xff0c 输入pip3 install you get xff0c 输入命令you get安装 you get 二 使用指令 1 在cmd exe窗口输入you get o 保存地
  • 微信小程序项目目录结构以及各个文件夹和文件的作用

    pages文件夹 xff0c utils文件夹 xff0c 全局文件app js文件 xff0c 全局文件app json文件 xff0c 样式app wxss文件 xff0c 项目配置文件project config json xff0c
  • 使用SSD mobilenet训练自己的数据集

    1 首先配置好环境 xff0c 我是用的的torch1 7 1 xff0c 和torchvision0 8 2版本 xff0c 可以直接根据作者给出的requirements txt文件进行其他安装包的配置 2 下载代码lufficc SS
  • C#第二天

    1 变量的作用 xff1a 存储数据 2 int number 表示在内存中开辟了一个整数类型的房间 xff0c 并且我们取名为 number number 61 50 xff1b 表示将50这个整数放到房间number中 3 声明变量的语
  • Visual studio 配置pyqt 转换资源文件 qrc to py

    整体方法与前人类似 xff0c 笔者在此进行一点补充 宇宙最强VisualStudio2017配置pyQt5用于python3 6的UI界面工具 Visual studio 2017下设置和使用pyQt5应注意的几个问题 xff08 主要参
  • PHP爬虫 爬取污染数据实例

    PHP爬虫 爬取污染数据实例 标签 xff08 空格分隔 xff09 xff1a PHP HTML 爬虫 抓取的目标页面 http www pm25 com city shenzhen html 抓取后输出的Json数组 xff1a htt
  • JDK11下载安装、JRE生成、环境配置

    一 下载 JDK11官网下载 xff1a Java SE Downloads Oracle Technology Network Oracle 选择自己合适的版本 二 安装 三 生成JRE 1 安装目录 找到jdk的安装目录 2 jdk11
  • 传统目标跟踪——光流法

    目录 一 光流法 二 LK光流法 2 1 实现原理 2 2 API 三 代码 四 总结 基于特征点的光流跟踪 xff0c 在目标上提取一些特征点 xff0c 然后在下一帧计算这些特征点的光流匹配点 xff0c 统计得到目标的位置 在跟踪的过
  • WSL win11下 Linux 子系统安装 无法解析服务器的名称或地址

    前言 想在win11上部署docker于是乎找到wsl xff0c 可是在powershell上安装总是报 无法解析服务器的名称或地址 的错误 网上冲浪一般解决方案为手动修改DNS 114 114 114 114 8 8 8 8 xff0c
  • ACCESS从零入门教程

    最近 xff0c 在公司实习自学了一款简单的access数据库软件 xff0c 下面是自己的一些学习心得过程 xff0c 供大家参考 一 access导入数据 两种方法 xff1a 1 直接复制 xff0c crtl c v即可 2 若数据
  • 基于ROS的自主充电路径规划

    以下是一个简单的基于ROS的自主充电路径规划的代码示例 xff0c 仅供参考 在 ROS 中创建一个包 xff08 package xff09 xff1a 充电路径规划的代码需要位于一个独立的包中 xff0c 可以使用 catkin cre
  • HDU第六场

    1002 https vjudge net contest 387998 题目 xff1a https vjudge net contest 387998 problem B 思路 xff1a 题意是给一个等式 xff0c 判断是否能在2

随机推荐

  • 解决服务器不能复制粘贴的方法

    1 打开远程的服务器 在服务器的任务栏随便一块空白处右击鼠标 选择 启动任务管理器 2 在打开的任务管理器中 我们找到 rdpclip exe 这个进程 如果没有找到就算了 3 找到这个进程后 选择 34 结束进程 34 4 然后再往服务器
  • 最小二乘法,最大似然估计什么情况下统一

    机器学习中 线性回归算法用到最小二乘法 逻辑回归算法用到最大似然估计 在推导梯度的过程中 发现结果一样 这是为何呢 目录 一 最小二乘法1 基本思想2 作用3 如何求解最小二乘法 二 最大似然估计1 概念2 似然估计的思想是3 如何求解最大
  • Linux 系统目录结构

    树状目录结构 bin xff1a bin 是 Binaries 二进制文件 的缩写 这个目录存放着最经常使用的命令 boot xff1a 这里存放的是启动 Linux 时使用的一些核心文件 xff0c 包括一些连接文件以及镜像文件 dev
  • MySql8.x版本my.cnf文件配置详解

    my cnf for 8 0版本 注意 xff1a 个别建议可能需要根据实际情况作调整 xff0c 请自行判断或联系我 xff0c 本人不对这些建议结果负相应责任 本配置文件主要适用于MySQL 8 0版本 client port 61 3
  • YouTube推荐系统

    The YouTube Video Recommendation System RecSys2010 实际的YouTube推荐系统在不断的改进 我们这里看到的是2010年RecSys会议上YouTube的一篇关于推荐的文章 xff0c 事实
  • Mysql配置文件/etc/my.cnf解析

    Mysql配置文件 etc my cnf解析 客户端设置 client port 61 3306 默认情况下 xff0c socket文件应为 usr local mysql mysql socket 所以可以ln s xx tmp mys
  • 数据结构:使用链栈实现回文判断

    题目 xff1a 回文判断 试写一个算法 xff0c 判断依次读入的一个以 64 为结束符的字母序列 xff0c 是否为形如 序列1 amp 序列2 模式的字符序列 其中序列1和序列2中都不含字符 amp xff0c 且序列2是序列1的逆序
  • Python 读取pdf

    好久没有更新 xff0c 主要是工作比较忙 抽空记录一个最近用到的东西 用python 读取pdf 话不多少 还是先上代码 span class token keyword import span pdfplumber span class
  • Python中的字符串相似度

    文章目录 一 Python字符串相似度二 Python相似度评估1 在计算图片的相似度时 xff0c 我自己用到过余弦距离2 欧式距离3 曼哈顿距离4 切比雪夫距离5 闵可夫斯基距离6 标准化欧氏距离7 马氏距离8 编辑距离 一 Pytho
  • 浅谈linux开发板用户登录之getty/login/passwd

    最近在排查一个关于用户登录的问题 xff0c 需要了解开发板启动以及远程登录进行用户名和密码验证背后的原理 经过查询学习 xff0c 简单总结如下 文章目录 前言一 Linux开发板登录机制二 getty login passwd1 get
  • 【实用技巧】rpm包下载,安装。获取rpm资源

    1 rpm包下载 我们使用yum install命令的时候一般下载下来会直接安装 xff0c 但是如果我们只想下载rpm包而不安装该怎么做呢 xff1f 安装 yum utils yum span class token function
  • 新手折腾wsl

    新手折腾wsl图形界面 本文记录一些本人 xff08 未学习Linux相关知识 xff09 折腾wsl踩过的坑 xff0c 以及参考的有效的解决方案 换源 这个搞过的都懂 xff0c 不翻墙的话 xff0c 用本身的那个源 xff0c 更新
  • Windows server

    显示win server信息 xff1a 进入cmd下输入systeminfo 启动控制台 没有功能可以添加 xff1a mmc 启动服务器管理器 xff1a services msc 进入防火墙的配置 xff1a wf msc 添加Win
  • 配置与管理DNS服务器

    实训目的 项目环境及要求 win2012 1 xff08 已经安装了long com域 xff09 xff08 并且是long com的域控 xff09 win2012 4 xff08 在这台服务器上部署DNS服务 xff09 win7 x
  • 配置与管理Web和FTP服务器

    实训目的 项目环境要求 win2012 1 xff1a 已经安装long com的域并且已经安装DNS win2012 4 xff1a 部署服务 win7 安装服务 添加功能 勾选 安装完成 进入IIS管理器 在此完成网站的创建和FTP的创
  • 公式推导

    公式推导 事件为相互独立的情况 xff1a n 个相互独立且服从相同分布的事件 X 1 X 2 X n xff0c 其标准差为 期望为 则总的的事件的期望和方差分别为 xff1a E X 1 43 X 2 43 X n
  • Windows 10 docker 容器添加新端口映射的方法与步骤

    在Docker容器已经创建后 xff0c 需要添加新的端口映射 xff0c 即对已经存在的Docker容器添加新的端口映射 xff0c 可以通过以下步骤来添加 xff0c 即通过修改配置文件的方法 1 Windows 10 下 Docker
  • 配置yum源挂载mount /dev/sr0 /iso报错mount: 在 /dev/sr0 上找不到媒体

    span class token punctuation span root 64 localhost span class token punctuation span span class token comment umount de
  • Debian之CA认证

    安装服务 root 64 debian etc chrony span class token comment apt install y openssl span 配置文件 root 64 debian etc chrony span c
  • 字符串内建函数

    find函数查找 strint example span class token operator 61 span span class token string 34 hello world good night 34 span inde