• 字符串截取 
>>>s = 'hello'
>>>s[0:3]
'he'
>>>s[:] #截取全部字符
'hello'
  • 消除空格及特殊符号
 
s.strip() #消除字符串s左右两边的空白字符(包括'\t','\n','\r','')

s.strip('0') #消除字符串s左右两边的特殊字符(如'0'),字符串中间的'0'不会删除
例如:
>>>s = '000hello00world000'
>>>s.strip('0')
'hello00world' s.strip('12')等价于s.strip('21')
例如:
>>>s = '12hello21'
>>>s.strip('12')
'hello' lstrip,rstrip 用法与strip类似,分别用于消除左、右的字符
 
  • 字符串复制
s1 = 'hello'
s2 = s1 # s2 = 'hello' 若指定长度
s1 = 'hello'
s2 = s1[0:2] #s2 = 'he'
  • 字符串连接 
 
s1 = 'hello'
s2 = 'world'
s3 = s1 + s2 #s3 = 'helloworld' 或者
import operator
s3 = operator.concat(s1,s2) #concat为字符串拼接函数
 
  • 字符串比较 
 
(1)利用operator模块方法比较(python3.X取消了cmd函数)
包含的方法有:
lt(a, b) ———— 小于
le(a, b) ———— 小于等于
eq(a, b) ———— 等于
ne(a, b) ———— 不等于
ge(a, b) ———— 大于等于
gt(a, b) ———— 大于 例子:
>>>import operator
>>>operator.eq('abc','edf') #根据ASCII码比较
Flase
>>>operator.gt('abc','ab')
True (2)关系运算符比较(>,<,>=,<=,==,!=)
>>>s1 = 'abc'
>>>s2 = 'ab'
>>>s1 > s2
True
>>>s1 == s2
False
 
  • 求字符串长度 
>>>s1 = 'hello'
>>>len(s1)
5
  • 求字符串中最大字符,最小字符 
>>>s1 = 'hello'
>>>max(s1) #求字符串s1中最大字符
'o'
>>>min(s1) #求字符串s2中最小字符
'e'
  • 字符串大小写转换 
 
主要有如下方法:
upper ———— 转换为大写
lower ———— 转换为小写
title ———— 转换为标题(每个单词首字母大写)
capitalize ———— 首字母大写
swapcase ———— 大写变小写,小写变大写
center(width,fillchar=None) ————将字符串用特定字符串填充至指定宽度
例子: 
>>>s1 = 'hello'
>>>s2 = 'WORLD'
>>>s3 = 'hello world'
>>>s1.upper()
'HELLO'
>>>s2.lower()
'world'
>>>s3.title()
'Hello World'
>>>s3.capitalize()
'Hello world'
>>>s3.title().swapcase()
'hELLO wORLD'
>>>s1.center(20,'*')
*******hello********
 
  • 字符串翻转
>>>s1 = 'hello'
>>>s1[::-1]
'olleh'
  • 字符串分割
 
split方法,根据参数进行分割,返回一个列表

例子:
>>>s1 = 'hello,world'
>>>s1.split(',')
['hello','world']
 
  • 字符串序列连接 
 
join方法:
语法为str.join(seq) #seq为元素序列 例子: >>>l = ['hello','world']
>>>str = '-'
>>>str.join(l)
'hello-world'
 
  • 字符串内查找 
 
find方法:
检测字符串内是否包含子串str
语法为:
str.find(str[,start,end]) #str为要查找的字符串;strat为查找起始位置,默认为0;end为查找终止位置,默认为字符串长度。若找到返回起始位置索引,否则返回-1 例子: >>>s1 = 'today is a fine day'
>>>s1.find('is')
6
>>>s1.find('is',3)
6
>>>s1.find('is',7,10)
-1
 
  • 字符串内替换 
 
replace方法:
把字符串中的旧串替换成新串
语法为:
str.replace(old,new[,max]) #old为旧串,new为新串,max可选,为替换次数 例子:
>>>s1 = 'today is a find day'
>>>s1.replace('find','rainy')
'today is a rainy day'
 
  • 判断字符串组成 
 
主要有如下方法:
isdigit ———— 检测字符串时候只由数字组成
isalnum ———— 检测字符串是否只由数字和字母组成
isalpha ———— 检测字符串是否只由字母组成
islower ———— 检测字符串是否只含有小写字母
isupper ———— 检测字符串是否只含有大写字母
isspace ———— 检测字符串是否只含有空格
istitle ———— 检测字符串是否是标题(每个单词首字母大写) 例子:
>>>s1 = 'hello'
>>>s1.islower()
True
>>>s1.isdigit()
False

(六)python3 字符串常用方法的更多相关文章

  1. python3字符串常用方法

    整型和布尔值的转换: bin -- 十进制转二进制 int("1101",2) -- 二进制转十进制 十进制转二进制的算法 除2 取余,获取的所有余数从下往上进行计算 二进制转十进 ...

  2. python基础3 字符串常用方法

    一. 基础数据类型 总览 int:用于计算,计数,运算等. 1,2,3,100...... str:'这些内容[]'    用户少量数据的存储,便于操作. bool: True, False,两种状态 ...

  3. python系列四:Python3字符串

    #!/usr/bin/python #Python3 字符串#可以截取字符串的一部分并与其他字段拼接var1 = 'Hello World!'print ("已更新字符串 : ", ...

  4. OC中的字符串常用方法

    OC中的字符串常用方法 OC中对字符串进行操作使用了Foundation框架中的NSString类(不可变).NSMutableString类(可变). NSString 1.创建字符串 [objc] ...

  5. python 字符串常用方法

    字符串常用方法 capitalize() String.capitalize() 将字符串首字母变为大写 name = 'xiaoming' new_name = name.capitalize() ...

  6. python3字符串

    Python3 字符串 Python字符串运算符 + 字符串连接 a + b 输出结果: HelloPython * 重复输出字符串 a*2 输出结果:HelloHello [] 通过索引获取字符串中 ...

  7. [转]python3字符串与文本处理

    转自:python3字符串与文本处理 阅读目录 1.针对任意多的分隔符拆分字符串 2.在字符串的开头或结尾处做文本匹配 3.利用shell通配符做字符串匹配 4.文本模式的匹配和查找 5.查找和替换文 ...

  8. python3字符串操作

    python3字符串操作 x = 'abc' y = 'defgh' print(x + y) #x+y print(x * ) #x*n print(x[]) #x[i] print(y[:-]) ...

  9. Python基础二_操作字符串常用方法、字典、文件读取

    一.字符串常用方法: name.captitalize()                       #字符串首字母大写 name.center(50,'*')                   ...

随机推荐

  1. 每周一算法之六——KMP字符串匹配算法

    KMP是一种著名的字符串模式匹配算法,它的名称来自三个发明人的名字.这个算法的一个特点就是,在匹配时,主串的指针不用回溯,整个匹配过程中,只需要对主串扫描一遍就可以了.因此适合对大字符串进行匹配. 搜 ...

  2. 2-3 Vue实例中的数据,事件和方法

    上节课模板是写在Vue的实例里面的,现在我们可以把它恢复出来.写在挂载点的内部,看起来会舒服一点.Vue的数据项,可以配置任意的数据名字. <!DOCTYPE html> <html ...

  3. jQuery setInterval倒计时精确到毫秒

    效果类似于:购物抢购倒计时-->在跳转N多个页面之后,倒计时间仍然正常显示. 思路: 结束时间是固定不变的(endTime),一直在改变的是当下的时间(curTime = new date()) ...

  4. 洛谷2019 3月月赛 T1

    题干 2019第一次月赛 我只有255pts T1还是比较水的... 海星 T1一道简单的模拟(就是有坑..导致很多人不能一次性AC 比如说我) _3个坑点 1.位数问题 2.-0 3.0... #i ...

  5. java 读取word

    读取word文件 import java.io.File; import java.io.FileInputStream; import java.io.InputStream; import org ...

  6. PowerDesigner 的使用教程

    PowerDesigner 的使用这两篇博客挺好,我也是跟着学习,就不再写了: 初步学习: http://www.cnblogs.com/huangcong/archive/2010/06/14/17 ...

  7. Android 性能优化(24)*性能工具之「Traceview,dmtracedump」Profiling with Traceview and dmtracedump :记录并查看函数调用栈*

    Profiling with Traceview and dmtracedump In this document Traceview Layout         Traceview工具界面介绍 T ...

  8. Spring Boot (32) Lock 本地锁

    平时开发中,有时会双击提交表单造成重复提交,或者网速比较慢时还没有响应又点击了按钮,我们在开发中必须防止重复提交 一般在前台进行处理,定义个变量,发送请求前判断变量值为true,然后把变量设置为fal ...

  9. Learning Face Age Progression: A Pyramid Architecture of GANs

    前言 作为IP模式识别的CNN初始模型是作为单纯判别式-模式识别存在的,并以此为基本模型扩展到各个方向.基本功能为图像判别模型,此后基于Loc+CNN的检测模型-分离式.end2end.以及MaskC ...

  10. 计算机网络(二)--HTTP详解

    Web相关内容都是存储在Web服务器上,Web服务器上使用的是http协议,因此也被成为http服务器.http的client.server构成了万维网的 基本组件 一.资源 1.URI: 统一资源标 ...