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

(1)切片操作:

str1="hello world!"

str1[1:3] <=> 'el'(左闭右开:即是从1到2)

str[:3] <=> 'hel'

str[2:] <=> 'llo world!'

(2)和Java中的字符串一样,不能直接改变字符串的值,更新字符串时候可以用切片技术:

str1="hello world!"

str1=str1[:1]+'python'+str1[1:] <=> 'hpythonello world!'

(3)capitalize():将字符串第一个字符大写

>>> str='hello world!'
>>> str.capitalize ()
'Hello world!'
>>>

(4)casefold():将整个字符串小写

>>> str1="Hello world!"
>>> str1.casefold ()
'hello world!'
>>>

(5)center(width):将整个字符串居中(如果不够width则用空格补充)

str1="Hello world!"

>>> str1.center(20)
' Hello world! '
>>>

(6)count(sub[,start[,end]]):sub从start到end出现的次数(默认是整个字符串)

str1="Hello world!"

>>> str1.count ('l',3)
2("Hello world!")
>>> str1.count ('l')
3("Hello world!")
>>> str1.count('l',3,6)
1("Hello world!")
>>>

(7)endswith(sub)判断是否是以哪个字符串结尾

str1="Hello world!"

>>> str1.endswith('orld!')
True("Hello world!")
>>>

(8)expandstabs():将字符串中的'\t'转换为空格

>>> str2='include world!'
>>> str2.expandtabs()
'include world!'
>>>

(9)find(sub[,start][,end]):查找字符串中子串从start到end出现的位置并返回下标

str1="Hello world!"

>>> str1.find('llo')
2("Hello world!")
>>> str1.find('llo',3,8)
-1
>>>

(10)isalnum():判断s是否是数字或者字母

str1="Hello world!"

>>> str1.isalnum()
False("Hello world!")
>>>

(11)isspace():判断是否是空格

>>> str=" "
>>> str.isspace()
True
>>>

(12)isdigit():判断是否都是数字组成

>>> str="12345dfgbhn"
>>> str.isdigit()
False("12345dfgbhn")
>>>

(13)isalpha():判断是否都是由字母组成的

>>> str='asdfghj'
>>> str.isalpha()
True
>>>

(14)islower():判断是否都是由小写字母组成的

>>> str='asdfghj'
>>> str.islower()
True
>>>

(15)istitle():判断是否是标题形式字符串(即是连续字符串只有第一个字母大写,其他都是小写,若是有空格,则每个分隔的字符串都满足此)

>>> str='Helloworld'
>>> str.istitle()
True
>>>

(16)isupper():判断是否都是由大写字母组成的

>>> str='HELLO WOLD'
>>> str.isupper()
True
>>>

(17)join(sub)

>>> str1="abc"
>>> str1.join('1234')
'1abc2abc3abc4'
>>>

(18)lstrip():去掉字符串左边所有空格

>>> str=" hello world!"
>>> str.lstrip()
'hello world!'
>>>

(19)rstrip():去掉字符串右边的空格

>>> str="hello world! "
>>> str.rstrip()
'hello world!'
>>>

(20)replace(old,[,new][,count]):将字符串中的old子串替换为new,替换count次

str='hello world!'

>>> str.replace('hello' ,'HELLO' ,2)
'HELLO world! '
>>>

(21)rfind(sub[,start][,end]):从右边开始查找字符串中子串从start到end出现的位置并返回下标(注意start和end是从左往右的,返回的也是从左到右的位置。)

>>> str="hello world!"
>>> str.rfind('d!',0,5)
-1
>>> str.rfind('d!')
10
>>>

(22)split(sep):将字符串用给定的标准分割,并且以列表形式返回分割后的元素组

>>> str="1,2,3,4"
>>> str.split(',')
['1', '2', '3', '4']
>>>

(23)startwith(sub[,start][,end]):判断从start到end是否以sub开头

>>> str.startswith('hel')
True
>>>

(24)strip():去掉字符串左右两边的空格

>>> str=' hello world! '
>>> str.strip()
'hello world!'
>>>

(25)swapcase():将字符串的大小写反转

>>> str="Hello world!"
>>> str.swapcase ()
'hELLO WORLD!'
>>>

(26)title()将字符串标题化(即是连续字符串的第一个字母大写,其他都是小写空格,分隔的字符串都遵循此规则)

>>> str="hello world!"
>>> str.title()
'Hello World!'
>>>

(27)translate(table)

>>> str="sssaabb"
>>> str.translate(str.maketrans('s','b'))
'bbbaabb'
>>>

(28)upper():将整个字符串都大写

>>> str="hello world!"
>>> str.upper()
'HELLO WORLD!'
>>>

(29)zfill(width):用'0'来填充不够的空格(是从左边开始填充)

>>> str="hello world! "
>>> str.zfill(20)
'00000hello world! '
>>>

(30)lower():将整个字符串都小写

>>> str="HELLO worldQ"
>>> str.lower()
'hello worldq'
>>>

(31)format()

>>> '{0} love {1}{2}'.format('I','my','home')
'I love myhome'
>>> '{0} love {1} {2}'.format('I','my','home')
'I love my home'
>>> '{a} love {b} {c}'.format(a='I',b='my',c='home')
'I love my home'

>>> '{0:.1f}{1}'.format(27.658,'GB')
'27.7GB'
>>>

(32)格式化:

>>> "%d+%d=%d" % (4,5,4+5)
'4+5=9'
>>>

>>> '%c' % 97
'a'
>>>

Python中的字符串操作总结(Python3.6.1版本)相关推荐

  1. 一句python,一句R︱python中的字符串操作、中文乱码、NaN情况(split、zip...)

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

  2. python中的字符串操作及注意事项

    1.mystr.find(str, start=0, end=len(mystr))          检测str是否包含在mystr中,如果是返回开始的索引值,否则返回-1.   mystr.rfi ...

  3. python中ignorecase_Python 字符串操作 starswitch() find() re.IGNORECASE replace() join()

    检测开头&结尾 开头:startswith() url = 'http://www.python.org' url.startswith('http') >>>True 结尾 ...

  4. python中怎么赋值,python中的赋值操作

    参考:https://www.cnblogs.com/andywenzhi/p/7453374.html?tdsourcetag=s_pcqq_aiomsg(写的蛮好) python中的赋值操作&qu ...

  5. python中unicode编码表_Python中的字符串操作和编码Unicode详解

    本文主要给大家介绍了关于 Python中的字符串操作和编码Unicode的一些知识,下面话不多说,需要的朋友们下面来一起学习吧. 字符串类型 str:Unicode字符串.采用''或者r''构造的字符 ...

  6. python中查找字符串_python中字符串操作--截取,查找,替换

    python中,对字符串的操作是最常见的,python对字符串操作有自己特殊的处理方式. 字符串的截取 python中对于字符串的索引是比较特别的,来感受一下: s = '123456789' #截取 ...

  7. python中对字符串进行左、中、右对齐操作

    python中对字符串的对齐操作一般有两种方式,具体如下: 1. ljust().rjust() 和 center()函数分别表示左对齐.右对齐.居中对齐 str.ljust(width[, fill ...

  8. 在Python中连接字符串的首选方法是什么?

    本文翻译自:Which is the preferred way to concatenate a string in Python? Since Python's string can't be c ...

  9. Python中的字符串与字符编码:编码和转换问题

    原文转载自:http://www.cnblogs.com/yyds/p/6171340.html 读后感:最近在跑实验,对于中文编码问题一直感到困扰,读完这篇文章以后,了解了Unicode编码的一些信 ...

最新文章

  1. 《Linux实践及应用》
  2. 全面了解 Nginx 主要应用场景
  3. SAP WM初阶之TO报表LX10 - Evaluation of movements per storage type
  4. HarmonyOS之后台代理定时提醒的功能使用
  5. 大数据WE阶段(十七)文件上传
  6. 量化交易,量化分析推荐书单
  7. DFS应用——遍历有向图+判断有向图是否有圈
  8. 快速搭建react项目骨架(按需加载、redux、axios、项目级目录等等)
  9. Nutanix的野心可不小!
  10. [转载] 跟着吴恩达学机器学习(Machine Learning) on Coursera 第一天
  11. proteus8找不到isis
  12. 蜘蛛采集单域名网站克隆镜像源码
  13. React 移动端`1px`像素边框
  14. scree VS tmux
  15. 当封号成为一种常态,网络营销人该何去何从?
  16. 1226. The Dining Philosophers (Leetcode 1226)
  17. 笑喷了,电视剧里的代码真能运行吗?
  18. 验证码_python
  19. springcloud官方文档,中英文双版
  20. 分区失败,在计算机中不能打开磁盘,但是在磁盘管理软件中能打开的解决方法

热门文章

  1. SSM+Druid实现动态多数据源切换(已实践)
  2. 从Dart列表中删除重复项的2种方法
  3. 3、SpringBoot整合MyBatis注解版及配置文件版
  4. html棍子英雄电脑版源码,英雄难过棍子关电脑版
  5. php自定义中文分词方法,PHPAnalysis中文分词类详解
  6. 月营收同比 10 倍增长,神策分析 1.8 推出英文版
  7. nginx反向代理https站点
  8. 一道关于 ARRAY 深度展开的面试题
  9. 通过OWA修改密码,提示您输入的密码不符合最低安全要求
  10. [LintCode] strStr [KMP brute force]