python learning 字符串方法
一、重点掌握的6种字符串方法:
1、join命令
功能:用于合并,将字符串中的每一个元素按照指定分隔符进行拼接
程序举例:
seq = ['1','2','3','4']
sep = '+'
v = sep.join(seq)
print(v)

test = "学习要思考"
t = '***'
v = t.join(test)
print(v)

2、split命令
功能:与join相反,将字符串拆分为序列
test = '1+2+3+4+5'
v = test.split('+')
print(v)

test = '/usr/bin/env'
v = test.split('/')
print(v)

3、find命令
功能:在字符串中寻找子串。如果找到,就返回子串的第一个字符索引,否则返回-1.
test1 = 'with a moo-moo here, and a moo-moo there'
v1 = test1.find('moo')
print(v1) test2 = "Monty Python's Flying Circus"
v2 = test2.find('Monty')
v3 = test2.find('Python')
v4 = test2.find('Flying')
v5 = test2.find('Zirquss')
print(v2)
print(v3)
print(v4)
print(v5)

可以指定搜索起点和终点
test = '### Get rich now!!! ###'
v = test.find('###', 1)
v1 = test.find('!!!')
v2 = test.find('!!!', 0, 16)
print(v)
print(v1)
print(v2)

4、strip命令:
功能:将字符串开头和结尾的空白(不包括中间的空白)删除,或者删除指定字符
test = '*** smart * fast * strong!!! ***'
v = test.strip(' *!')
print(v)

names = ['gumby', 'smith', 'jones']
name = 'gumby '
if name in names:
print('Found it')
else:
print('Not exist')
if name.strip() in names:
print('Found it')

5、upper命令和lower命令:
test = "aLex"
v1 = test.upper()
v2 = test.lower()
print(v1)
print(v2)

二、字符串常见四种应用:
1、索引,下标 获取字符串中的某个字符
test = "alex"
v = test[2]
print(v)

2、切片,索引范围 0 =< <1
test = "alex"
v = test[0:2]
print(v)

3、len获取当前字符串中由几个字符组成
test = "alex"
v = len(test)
print(v)

test = "圣诞节爱范娜"
index = 0
while index < len(test):
v = test[index]
print(v)
index += 1
print('=======')

4、for循环:(非常重要)
for 变量名 in 字符串:
变量名
for循环,索引,切片
test = "圣诞节爱范娜"
for item in test:
print(item)

range命令:帮助创建连续数字,,通过设置步长来指定不连续数字
v = range(0,100)
for item in v:
print(item)

v = range(0,10,2)
for item in v:
print(item)

************例题:将文字,对应的索引打印出来**************
test = input(">>>")
v = range(0,len(test))
for item in v:
print(item,test[item])

*********************
注意:
********************************************
字符串一旦创建,不可修改
一旦修改或者拼接,都会造成重新生成字符串
********************************************
python learning 字符串方法的更多相关文章
- python拼接字符串方法汇总
python拼接字符串一般有以下几种方法: 1.直接通过(+)操作符拼接 s = 'Hello'+' '+'World'+'!' print(s) 输出结果:Hello World! 这种方式最常用. ...
- Python常见字符串方法函数
1.大小写转换 S.lower() S.upper() 前者将S字符串中所有大写字母转为小写,后者相反 S.title() S.capitalize() 前者返回S字符串中所有单词首字母大写且其他字母 ...
- python之字符串方法upper/lower
1.描述: upper():用于将字符串全部转换为大写字母 lower():用于将字符串全部转换为小写字母 2.语法 str.upper() str.lower() 3.返回值 upper()或low ...
- Python之字符串方法
def capitalize(self): # 第一个字符变大写 def center(self, width, fillchar=None): # 内容居中,两端可指定内容填充 def count( ...
- python,字符串方法
1.capitalize() 首字母大写 text = "hello word" text2 = text.capitalize() print(text2) 2.1.casefo ...
- python中字符串方法总结
定义一个空字符串: a=' '; s.strip() #去空格 s.upper()#全部转换成大写: s.lower()# 全部转换成小写: s.isdigit()#判断字符串是否只有数字组成:返回t ...
- Python中的字符串方法
Python中的字符串方法 字符串类即str提供了许多有用的方法来操纵字符串.具体来说,我们将讨论如下的方法. 搜索字符串内的子字符串. 测试字符串. 格式字符串. 转换字符串. 回顾前面的章节,方法 ...
- python字符串方法的简单使用
学习python字符串方法的使用,对书中列举的每种方法都做一个试用,将结果记录,方便以后查询. (1) s.capitalize() ;功能:返回字符串的的副本,并将首字母大写.使用如下: >& ...
- Python 字符串方法详解
Python 字符串方法详解 本文最初发表于赖勇浩(恋花蝶)的博客(http://blog.csdn.net/lanphaday),如蒙转载,敬请保留全文完整,切勿去除本声明和作者信息. ...
随机推荐
- cartographer 点云同步处理
1.点云同步处理的类 RangeDataCollator class RangeDataCollator { public: explicit RangeDataCollator( const st ...
- vCenter Server 6 Standard
准备环境和工具: 三台 ESXi 6.0主机: 准备一台Windows Server 2008 R2系统的虚拟机: VMware-VIM-all-6.0.0.iso 软件下载地址 链接: https: ...
- 【easy】power of 2,3,4
============================== 2的幂次 ================================ 最佳解法 如果一个数是2的次方数的话,根据上面分析,那么 ...
- ALU底层方法及计算机整数加减乘除模拟
ALU是计算机CPU的核心,即 算术逻辑单元(arithmetic and logic unit)ALU有几大功能,是计算机计算最基础的功能:1.算术运算:包含加法.减法等2.逻辑运算:主要是布尔运算 ...
- Vue实战笔记
1.组件的属性 例子: <template> <div class="hello"> <test-props name="demo" ...
- Vofuria ARCamera相机问题
想要发射 射线 Camera.allCameras[0] 该语句来选择相机:Camera.main 此语句只能找到Tag为MainCamera的相机:
- 论http弥补技术ajax、comet、SPADY、websocket
为什么要弥补http?http有什么缺陷? 查询资料中
- Java的家庭记账本程序(I)
日期:2019.2.26 博客期:037 星期二 Part 1: 今天使用新的"radio"标签,将搜索方式的选则内容更改了,如下图,不过,研究了很久的数据库连接也还是没有成功!嗯 ...
- echarts将图表Y坐标刻度设置成只显示整数
echarts的配置项中没有直接将坐标刻度强制设为整数的选项,但可以通过minInterval属性将刻度以整数形式显示,在配置项的yAxis对象中添加属性: minInterval: 1 表示将刻度的 ...
- tensorflow例子-【老鱼学tensorflow】
本节主要用一个例子来讲述一下基本的tensorflow用法. 在这个例子中,我们首先伪造一些线性数据点,其实这些数据中本身就隐藏了一些规律,但我们假装不知道是什么规律,然后想通过神经网络来揭示这个规律 ...