字符串操作

#coding:utf-8
#/usr/bin/python
"""
2018-11-03
dinghanhua
字符串操作
""" '''字符串是字符元素组成的序列
序列可使用索引,索引:从前往后 0 1 2...,从后往前-1,-2...
'''
str ='i love python'
'''长度len(str)'''
print('length of str:', len(str))
'''索引str[index]
index从0到len()-1''' print(str[0],str[-1],str[3])
#print(str[100]) # 索引越界IndexError: string index out of range print('遍历输出每个字符:')
for index in range(0,len(str)):
print(str[index]) #0,len-1 print('反向输出: ')
#for
for index in range(1,len(str)+1):
print(str[index*-1]) #-1到-len
#while
i=-1
while i>=-len(str):
print(str[i])
i-=1
'''切片
str[头索引:尾索引:步长],
步长默认1,包含头不包含尾
切片不改变原字符串
'''
slice = str[:3] #0-2
print(str,slice)
print(str[:],str[::2],str[5:],str[2:7],str[1:-1])
print('越界取: ',str[3:100]) print(str is str[:]) #不可变类型地址相同 '''字符串翻转'''
reversestr = str[::-1]
print(reversestr)
''' 'string'.join(str) 用string将str每个字符连接起来'''
strjoin = '~'.join(str)
print(strjoin)
'''* 字符串重复输出'''
strmul = str*3
print(strmul)
print('-'*50) #分割线
'''字符替换'''
strreplace = strjoin.replace('~','--',7) #替换最多7次
print(strreplace)
'''查找字符或子串'''
index = str.find('python')
print(index) #找到了返回首个匹配字串索引
index = str.find('python',7,10) #从索引7到10之间找
print(index) #没找到返回-1 index=str.index('o')
print(index) #找到了返回首个匹配字串索引
#index=str.index('oll')
#print(index) #找不到抛出异常ValueError: substring not found
'''字符串分割'''
li = strreplace.split('--')
print(li)
li = strreplace.split() #默认空格
print(li)
'''将id参数值替换成100'''
str = 'id=2&random=35318759314'
'''找到id参数的开始索引和结束索引,切片,替换'''
begin_index = str.find('id=')
if begin_index != -1:
end_index = str.find('&',begin_index) #从id=后开始查找&
strnew = str.replace(str[begin_index : end_index],'id=100') #切片然后替换
print(strnew)
'''分割,查找替换,连接'''
li = str.split('&')
print(li)
for i in (0,len(li)):
if li[i].startswith('id='):
li[i] = 'id=100'
break
print(li)
strnew2 = '&'.join(li)
print(strnew2)
'''大小写、去空格、内容判定等'''
print(str.upper(),str.isupper(),
str.lower(),str.islower(),
str.capitalize(), str.istitle() ) print(' test '.strip(),
' test '.lstrip(),
' test '.rstrip()) str='1314432 target test tick '
print(str.isalnum(),
str.isalpha(),
str.isdigit(),
str.isspace())
print(str.count(''),
str.startswith(''),
str.endswith('f'))

练习题:

#找出第一个不重复的字符
for char in str:
if str.count(char) == 1:
print(char,str.index(char))
break #去除重复字符
strnew = ''
for char in str:
if char not in strnew:
strnew += char
print(strnew)

python入门7 字符串操作的更多相关文章

  1. Python入门之 字符串操作,占位符,比较大小 等

    Python  字符串 常用的操作 切片 左包括右不包括的原则 ________________ 比较字符串大小 eg: cmp("a",'b')   -1第一个比第二个小  0 ...

  2. Python入门篇-文件操作

    Python入门篇-文件操作 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.文件IO常用操作 open:打开 read:读取 write:写入 close:关闭 readlin ...

  3. Python中的字符串操作总结(Python3.6.1版本)

    Python中的字符串操作(Python3.6.1版本) (1)切片操作: str1="hello world!" str1[1:3] <=> 'el'(左闭右开:即是 ...

  4. 一句python,一句R︱python中的字符串操作、中文乱码、NaN情况

    一句python,一句R︱python中的字符串操作.中文乱码.NaN情况 先学了R,最近刚刚上手Python,所以想着将python和R结合起来互相对比来更好理解python.最好就是一句pytho ...

  5. 【Python自动化Excel】Python与pandas字符串操作

    Python之所以能够成为流行的数据分析语言,有一部分原因在于其简洁易用的字符串处理能力. Python的字符串对象封装了很多开箱即用的内置方法,处理单个字符串时十分方便:对于Excel.csv等表格 ...

  6. [Python Study Notes]字符串操作

    字符串操作 a.字符串格式化输出 name = "liu" print "i am %s " % name     #输出: i am liu   PS: 字符 ...

  7. (Python基础)字符串操作

    以下是我在学习过程中用的一些常用字符串操作的相关列子和具体注释,感兴趣的可以自己动手试试看 #字符串操作 name = 'my name is keep' print(name.capitalize( ...

  8. python入门6 字符串拼接、格式化输出

    字符串拼接方式    1  使用 + 拼接字符串 2 格式化输出:%s字符串 %d整数 %f浮点数 %%输出% %X-16进制 %r-原始字符串 3 str.format() 代码如下: #codin ...

  9. 一句python,一句R︱python中的字符串操作、中文乱码

    先学了R,最近刚刚上手python,所以想着将python和R结合起来互相对比来更好理解python.最好就是一句python,对应写一句R. pandas可谓如雷贯耳,数据处理神器. 以下符号: = ...

随机推荐

  1. ecmall模板编辑中的标题如何自定义读取

    碰见了一个问题,刚上线的ecmall项目.客户说标题不要商城首页这四个字. 我去源码里找,找了半天才找到. 问题描述如下: 找到title的最原始模板themes\mall\tmall\top.htm ...

  2. PIE SDK K-Means分类

    1.算法功能简介 K-Means 算法的基本思想是:以空间中 k 个点为中心进行聚类,对最靠近他们的对象归类.通过迭代的方法,逐次更新各聚类中心的值,直至得到最好的聚类结果. 算法首先随机从数据集中选 ...

  3. Linux acpi off学习的必要

    ACPI是Intel(i386,x86_64,IA64)平台的标准固件规范,绝大部分OS需要从BIOS得到的信息都可以从ACPI得到,并且现在的趋势是未来的任何新的特性相关的信息都只能从ACPI得到. ...

  4. shell 命令之bind,enable,ulimit

    1.bind 在shell中,内建(builtin)命令bind,格式如下: bind [-m keymap] [-lpsvPSVX] bind [-m keymap] [-q function] [ ...

  5. opencv + ffmpeg

    opencv2.4.13 与 ffmepg 3.0 一起是可以安装成功的.注意编译ffmpeg时, ./configure   --enable-shared 否则会报错. 另外,把以上组合换成ope ...

  6. linux中Python源码安装和配置

    安装 首先获取安装包,此处版本为3.7 wget https://www.python.org/ftp/python/3.7.0/Python-3.7.0.tgz 解压 tar xvf Python- ...

  7. 使用 Ninject

    在[ASP.NET MVC 小牛之路]系列上一篇文章(依赖注入(DI)和Ninject)的末尾提到了在ASP.NET MVC中使用Ninject要做的两件事情,续这篇文章之后,本文将用一个实际的示例来 ...

  8. 03-struts2获得servetAPI

    1 原理 三个域合一的时候相同的键值对以小的域为准.ActionContext 对象创建:每次请求的时候都会创建一个与请求对应的 ActionContext 对象.ActionContext 销毁:请 ...

  9. 新建maven工程index.jsp页面报错

    引入servlet依赖jar <dependency><groupId>javax.servlet</groupId><artifactId>servl ...

  10. Linux systemd资源控制初探

    Linux systemd资源控制初探 本文记录一次cgroup子目录丢失问题,并简单探索了Linux systemd的资源控制机制. 问题现象 我们希望通过systemd拉起服务并通过cgroup限 ...