python中的字符串学习
# 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中的字符串学习的更多相关文章
- Python中Unicode字符串
Python中Unicode字符串 字符串还有一个编码问题. 因为计算机只能处理数字,如果要处理文本,就必须先把文本转换为数字才能处理.最早的计算机在设计时采用8个比特(bit)作为一个字节(byte ...
- Python中的字符串处理
Python转义字符 在需要在字符中使用特殊字符时,python用反斜杠(\)转义字符.如下表: 转义字符 描述 \(在行尾时) 续行符 \\ 反斜杠符号 \' 单引号 \" 双引号 \a ...
- python中修改字符串的几种方法
在Python中,字符串是不可变类型,即无法直接修改字符串的某一位字符.因此改变一个字符串的元素需要新建一个新的字符串.常见的修改方法有以下4种. 方法1:将字符串转换成列表后修改值,然后用join组 ...
- python中根据字符串导入模块module
python中根据字符串导入模块module 需要导入importlib,使用其中的import_module方法 import importlib modname = 'datetime' date ...
- python中的字符串
一.在python中,字符串是不可变类型 通过以下代码说明: >>> s = 'hello, world' >>> id(s) 2108634288304 > ...
- 【转】Python中的字符串与字符编码
[转]Python中的字符串与字符编码 本节内容: 前言 相关概念 Python中的默认编码 Python2与Python3中对字符串的支持 字符编码转换 一.前言 Python中的字符编码是个老生常 ...
- python中confIgparser模块学习
python中configparser模块学习 ConfigParser模块在python中用来读取配置文件,配置文件的格式跟windows下的ini配置文件相似,可以包含一个或多个节(section ...
- Python中常见字符串去除空格的方法总结
Python中常见字符串去除空格的方法总结 1:strip()方法,去除字符串开头或者结尾的空格>>> a = " a b c ">>> a.s ...
- Python中的字符串方法
Python中的字符串方法 字符串类即str提供了许多有用的方法来操纵字符串.具体来说,我们将讨论如下的方法. 搜索字符串内的子字符串. 测试字符串. 格式字符串. 转换字符串. 回顾前面的章节,方法 ...
随机推荐
- 深入理解Redis 数据结构—字典
字典,又称为符号表.关联数组或映射,是一种用于保存键值对的抽象数据结构.在字典中,一个键可以和一个值进行关联,这些关联的键和值称为键值对.键值对中键是唯一的,我们可以根据键key通过映射查找或者更新对 ...
- java后台生成文件给前端下载(response输出流)
1.设置 ContentType response.setContentType("application/x-download"); 2.设置文件名,并指定编码格式 fileNa ...
- Python入门系列(三)一学就会-基础数据类型
数据类型 您可以使用type()函数获取任何对象的数据类型. x = 5 print(type(x)) 数字类型 x = 1 # int y = 2.8 # float z = 1j # comple ...
- KingbaseES R6 集群测试job管理测试
案例说明: 本案例参考<Job And Schedule (V8R6C4)>(https://www.cnblogs.com/kingbase/p/15194227.html)单实例环境下 ...
- Spring集成测试
Spring 集成测试 需要再类的头部加入 @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration({"classpath ...
- Java开发学习(三十三)----Maven私服(一)私服简介安装与私服分类
一.私服简介 团队开发现状分析 (1)张三负责ssm_crm的开发,自己写了一个ssm_pojo模块,要想使用直接将ssm_pojo安装到本地仓库即可 (2)李四负责ssm_order的开发,需要用到 ...
- 全志H616基于官方外设开发-蜂鸣器
#include <stdio.h> #include <wiringPi.h> #include <unistd.h> #define BEEP 0 //设置针脚 ...
- Java 加载、编辑和保存WPS表格文件(.et/.ett)
WPS表格文件是金山开发的专门用于处理表格数据的Office工具,属于WPS Office中WPS文字.WPS表格和WPS演示三大功能模块之一.通常以.et和.ett作为文件后缀.我们在通过后端来操作 ...
- Loki日志系统基础知识
文章摘抄转载自:https://lluozh.blog.csdn.net/article/details/111027998 Loki 日志系统由以下3个部分组成: loki是主服务器,负责存储日志和 ...
- Fluentd部署:错误排查
介绍一下排查Fluentd运行时错误的几种方法. 查看日志 如果感觉Fluentd运行异常,请先查看日志.td-agent安装后,默认日志存放在/var/log/td-agent/td-agent.l ...