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提供了许多有用的方法来操纵字符串.具体来说,我们将讨论如下的方法. 搜索字符串内的子字符串. 测试字符串. 格式字符串. 转换字符串. 回顾前面的章节,方法 ...
随机推荐
- ASP.Net Core异步编程
ASP.Net Core异步编程 概念 什么是异步编程? 异步编程是可以让程序并行运行的一种手段,其可以让程序中的一个工作单元与主应用程序线程分开独立运行,并且在工作单元运行结束后,会通知主应用程序线 ...
- 云服务器上搭建cobalt strike遇到的一些小问题
一.前言: 当你兴高采烈的买了一台云服务器,迫不及待地想去搭建传说中的神器cobalt strike的时候,你可能会遇到以下的一些小问题,这里我会列出对应的解决方法. 二.遇到的一些小问题 1.上传文 ...
- 【java】学习路径27-HashSet、TreeSet,HashMap
学习路径20-27的所有源代码都可以在此下载 https://www.aliyundrive.com/s/cg8jTRbg6vy HashSet.TreeSet中,Set表示集合,特性在于:无序的.不 ...
- Springboot连接数据库(解决报错2)
好家伙, 新建项目,不出意外的话总是会出点意外的 第一天正常运行,第二天就炸了. 1.看报错 百度一下找解决方案 试着将 application.properties中的 com.mysql.jdbc ...
- 第三十六篇:vue3响应式(关于Proxy代理对象,Reflect反射对象)
好家伙,这个有点难. 1.代理对象Proxy Proxy 对象用于创建一个对象的代理,从而实现基本操作的拦截和自定义(如属性查找.赋值.枚举.函数调用等). 拦截对象中任意属性的变化,包括:查get, ...
- Memlab,一款分析 JavaScript 堆并查找浏览器和 Node.js 中内存泄漏的开源框架
Memlab 是一款 E2E 测试和分析框架,用于发现 JavaScript 内存泄漏和优化机会. Memlab 是 JavaScript 的内存测试框架.它支持定义一个测试场景(使用 Puppete ...
- Echarts中tooltip解决显示指定数据
今天开发中遇到一个问题,echarts图表触摸x轴触发tooltip会将x轴上所有的数据展示出来,但是有些场合只需要展示某些数据就可以,并不需要全部展示,如下图: 这里警戒线因为需要开关,所以使用填充 ...
- 在 Kubernetes 容器集群,微服务项目最佳实践
转载自:https://mp.weixin.qq.com/s/WYu3gDwKKf06f_FYbO9YRg 本文主要介绍我个人在使用 Kubernetes 的过程中,总结出的一套「Kubernetes ...
- Keepalived+HAProxy 搭建高可用负载均衡
转载自:https://mp.weixin.qq.com/s/VebiWftaRa26x1aA21Jqww 1. 概述 软件负载均衡技术是指可以为多个后端服务器节点提供前端IP流量分发调度服务的软件技 ...
- ProxySQL 配置ProxySQL
转载自:https://www.jianshu.com/p/212397a1be67 假定你已经对ProxySQL的架构有所了解.本文对ProxySQL的所有配置都是使用Admin管理接口完成的,该管 ...