# 1.字符串的下标(索引)
# 取字符串中的子串
print('1.字符串的下标(索引)')
str1 = 'PYTHON'
print(str1[0])
print(str1[-4]) # 2.字符串的切片 起始值:终止值:步长
print()
print('2.字符串的切片') print('2.1使用正索引')
str1 = 'PYTHON'
print(str1[::]) # 取全部
print(str1[:]) # 一个冒号是默认步长为1
print(str1[:3]) # 起始值默认为0,包头不包尾
print(str1[2:4]) # 取中间 包头不包尾
print(str1[4:]) # 终止值默认为最大 # 2.2 使用负索引
print()
print('2.2使用负索引')
str1 = 'PYTHON'
print(str1[-3:-1]) # 取中间
print(str1[-4:]) # 取后面
print(str1[:-2]) # 取前面 # 2.3步长
print()
print('2.3步长')
str1 = 'PYTHON'
print(str1[::2])
print(str1[::-1]) # 注意啦,步长为负的时候,起始值默认是-1 # 3.字符串的常用操作
print()
print('3.字符串的常用操作') # 3.1 查找
print()
print('3.1查找') # 3.11 str1.find()
# str1.find(查找的字符串,开始位置,结束位置)
# 找到了,则返回索引值
# 找不到,则返回-1
print()
print('3.11 str1.find()')
str1 = 'hello world and Python and Windows'
print(str1.find('world'))
print(str1.find('worlds'))
print(str1.find('and')) # 只找了最开始的and
print(str1.find('and', 13, 50)) # 找到了第二个and # 3.12 str1.index()
# 找到了,则返回索引值
# 找不到,则报错
print()
print('3.12 str1.index()')
print(str1.index('and'))
# print(str1.index('ands')) #会报错 # 3.13 str1.count() 查找字符串出现的次数
print()
print('3.13 str1.count()')
print(str1.count('and'))
print(str1.count('ands')) # 没出现过这个字符串,那就是出现了0次呗
print(str1.count('a')) # 一个字符也是字符串哦 # 3.2.修改
print()
print('3.2 修改')
# 3.21 替换
# str1.replace(旧的,新的,替换的次数)
# 字符串是一个不可变类型
print()
print('3.21 替换 str1.replace()')
str1 = 'hello world and Python and Windows'
str1.replace('and', 'he')
print(str1) # 但这里str1并没有改变
new_str1 = str1.replace('and', 'he') # 替换的次数默认全部
print(new_str1)
new1_str1 = str1.replace('and', 'he', 1) # 只替换了一次
print(new1_str1) # 3.22 分割
# str1.split() 以某个字符进行分割,放回一个列表
# 默认以空格分割,会删去空格
print()
print('3.22 分割 str1.split')
print(str1.split())
print(str1.split('and')) # 以and分割,会删去and,保留空格的! # 3.23 连接
# str1.join(列表) # 正好 连接 分割 后产生的列表
print()
print('3.23 str1.join()')
new_str = 'he'.join(str1.split('and')) # 用'he'连接
print(new_str) # 3.24 大小写转换
print()
print('3.24 大小写转换')
str1 = 'hello world and Python and Windows'
print(str1.lower()) # 所有字母转换为小写
print(str1.upper()) # 所有字母转换为大写
print(str1.title()) # 把每个单词的首个字母大写
print(str1.capitalize()) # 将字符串的第一个单词的首字母转换成大写,其他变成小写 # 3.25 删除空格(空白符)
print()
print('3.25 删除空格(空白符)')
str2 = ' hello world and Python and Windows '
print(str2.strip()) # 删除字符串两边的空格
print(str2.lstrip()) # 只删除字符串左边的空格
print(str2.rstrip()) # 只删除字符串右边的空格 # 3.3 判断
print()
print('3.3 判断')
str1 = 'hello world and Python and Windows'
print(str1.startswith('hello')) # 判断是否以某个字符串开头
print(str1.startswith('hellos'))
print(str1.endswith('Windows')) # 判断是否以某个字符串结尾 str2 = '123'
print(str2.isdigit()) # 判断字符串里面是否全部是数字
print(str2.isalpha()) # 判断字符串里面是否全部是字母 # 3.4 填充
print()
print('3.4 填充')
str2 = 'abcde'
# str2.ljust(填充后字符串的长度,填充的字符) 左填充
# str2.rjust() 右填充
# str2.center() 中间填充
# str2.zfill() 0填充
print(str2.ljust(10, '0')) # 左填充,填充后字符串在左
print(str2.center(10, 'X'))
print(str2.zfill(10)) # 今日练习
# 创建一个字符串'helloworld'
# (1)将l用A替换
# (2)将所有的o删除
# (3)将操作后的字符串结果打印输出
print()
print('今日练习')
str_1 = 'helloworld'
new_str_1 = str_1.replace('l', 'A', 2)
# 字符串是一个不可变类型,
# 所以要用一个新的字符串存储替换后的字符串
print(''.join(new_str_1.split('o')))

python中的字符串学习的更多相关文章

  1. Python中Unicode字符串

    Python中Unicode字符串 字符串还有一个编码问题. 因为计算机只能处理数字,如果要处理文本,就必须先把文本转换为数字才能处理.最早的计算机在设计时采用8个比特(bit)作为一个字节(byte ...

  2. Python中的字符串处理

    Python转义字符 在需要在字符中使用特殊字符时,python用反斜杠(\)转义字符.如下表: 转义字符 描述 \(在行尾时) 续行符 \\ 反斜杠符号 \' 单引号 \" 双引号 \a ...

  3. python中修改字符串的几种方法

    在Python中,字符串是不可变类型,即无法直接修改字符串的某一位字符.因此改变一个字符串的元素需要新建一个新的字符串.常见的修改方法有以下4种. 方法1:将字符串转换成列表后修改值,然后用join组 ...

  4. python中根据字符串导入模块module

    python中根据字符串导入模块module 需要导入importlib,使用其中的import_module方法 import importlib modname = 'datetime' date ...

  5. python中的字符串

    一.在python中,字符串是不可变类型 通过以下代码说明: >>> s = 'hello, world' >>> id(s) 2108634288304 > ...

  6. 【转】Python中的字符串与字符编码

    [转]Python中的字符串与字符编码 本节内容: 前言 相关概念 Python中的默认编码 Python2与Python3中对字符串的支持 字符编码转换 一.前言 Python中的字符编码是个老生常 ...

  7. python中confIgparser模块学习

    python中configparser模块学习 ConfigParser模块在python中用来读取配置文件,配置文件的格式跟windows下的ini配置文件相似,可以包含一个或多个节(section ...

  8. Python中常见字符串去除空格的方法总结

    Python中常见字符串去除空格的方法总结 1:strip()方法,去除字符串开头或者结尾的空格>>> a = " a b c ">>> a.s ...

  9. Python中的字符串方法

    Python中的字符串方法 字符串类即str提供了许多有用的方法来操纵字符串.具体来说,我们将讨论如下的方法. 搜索字符串内的子字符串. 测试字符串. 格式字符串. 转换字符串. 回顾前面的章节,方法 ...

随机推荐

  1. UVA1306 The K-League(最大流)

    题面 有 n n n 支队伍进行比赛,每支队伍需要打的比赛数目相同. 每场比赛恰好一支队伍胜,另一支败. 给出每支队伍目前胜的场数 w i w_i wi​ 和败的场数(没用),以及每两个队伍还剩下的比 ...

  2. Windows权限维持总结

    windows权限维持 注册服务 sc create 服务名 binpath= "cmd.exe /k 木马路径" start="auto" obj=" ...

  3. 强大多云混合多K8S集群管理平台Rancher入门实战

    @ 目录 概述 定义 为何使用 其他产品 安装 简述 规划 基础环境 Docker安装 Rancher安装 创建用户 创建集群 添加Node节点 配置kubectl 创建项目和名称空间 发布应用 偏好 ...

  4. ZooKeeper 组件安装配置

    ZooKeeper 组件安装配置 下载和安装 ZooKeeper ZooKeeper最新的版本可以通过官网 http://hadoop.apache.org/zookeeper/ 来获取,安装 Zoo ...

  5. Android开发2021.3.9日【模拟器路径】【外观字体】【简单快捷键】

    一. 1.模拟器存储路径 D:\Android\SDK\platforms(在本人的dell上) 2.使用软件 Android Studio4.2 3.注意事项 (1)修改JDK的路径为自己下载的JD ...

  6. losf命令详解

    一.概念:lsof全名list opened files,也就是列举系统中已经被打开的文件,进程打开的端口(TCP.UDP).linux环境中,任何事物都是文件,设备是文件,目录是文件,甚至socke ...

  7. SpringBoot源码学习1——SpringBoot自动装配源码解析+Spring如何处理配置类的

    系列文章目录和关于我 一丶什么是SpringBoot自动装配 SpringBoot通过SPI的机制,在我们程序员引入一些starter之后,扫描外部引用 jar 包中的META-INF/spring. ...

  8. day02-代码实现01

    多用户即时通讯系统02 4.编码实现01 4.1功能实现-用户登录 4.1.1功能说明 因为还没有学习数据库,我们人为规定 用户名/id = 100,密码为 123456 就可以登录,其他用户不能登录 ...

  9. .env[mode]文件中如何添加注释

    前言 Vue-Cli 允许我们在项目根目录创建.env.[mode]文件来设置一些打包编译的启动参数,通过执行脚本的时候加mode参数,指定不同环境需要加载的配置文件 形如: .env.prod NO ...

  10. 使用 APM 中的 Service Map 了解和调试应用程序

    文章转载自:https://blog.csdn.net/UbuntuTouch/article/details/118667839