Python中的字符串操作总结(Python3.6.1版本)
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版本)的更多相关文章
- 一句python,一句R︱python中的字符串操作、中文乱码、NaN情况
一句python,一句R︱python中的字符串操作.中文乱码.NaN情况 先学了R,最近刚刚上手Python,所以想着将python和R结合起来互相对比来更好理解python.最好就是一句pytho ...
- 一句python,一句R︱python中的字符串操作、中文乱码
先学了R,最近刚刚上手python,所以想着将python和R结合起来互相对比来更好理解python.最好就是一句python,对应写一句R. pandas可谓如雷贯耳,数据处理神器. 以下符号: = ...
- python中的字符串操作
#!/usr/bin/python # -*- coding: UTF-8 -*- ''' str.capitalize() ''' str = 'this is a string example' ...
- 【转】Python中的字符串与字符编码
[转]Python中的字符串与字符编码 本节内容: 前言 相关概念 Python中的默认编码 Python2与Python3中对字符串的支持 字符编码转换 一.前言 Python中的字符编码是个老生常 ...
- Python中的json操作
Python中的json操作 标签(空格分隔): python 编码 json 字符串前缀问题 字符串前缀可以有r,u r:表示原始(raw)字符串,比如'\n'不会被转义.常用于正则. u:表示un ...
- java入门学习笔记之2(Java中的字符串操作)
因为对Python很熟悉,看着Java的各种字符串操作就不自觉的代入Python的实现方法上,于是就将Java实现方式与Python实现方式都写下来了. 先说一下总结,Java的字符串类String本 ...
- python中的字符串
一.在python中,字符串是不可变类型 通过以下代码说明: >>> s = 'hello, world' >>> id(s) 2108634288304 > ...
- Python中通过open()操作文件时的文件中文名乱码问题
最近在用Python进行文件操作的时候,遇到创建中文文件名的乱码问题. Python默认是不支持中文的,一般我们在程序的开头加上#-*-coding:utf-8-*-来解决这个问题,但是在我用open ...
- python中OS模块操作文件和目录
在python中执行和操作目录和文件的操作是通过内置的python OS模块封装的函数实现的. 首先导入模块,并查看操作系统的类型: >>> import os os.name # ...
随机推荐
- javascript高级程序设计第三章
看后总结: 1.区分大小写 2.标识符是有字母下划线$开头,并有字母.下划线.数字.美元符号组成. 3.建议用驼峰法命名标识符. 4.注释: 单行:// 多行: /* */ 5.严格模式: 在js ...
- Android——点击对话框上按钮不关闭对话框
有时候我没可能需要在点击按钮进行一些检测,但是并不想关闭次对话框(系统默认点击任何一个按钮则关闭对话框),处理方法如下:在点击事件下添加如下代码: try { Field field = dialog ...
- 从一个activity返回
页面之间的跳转有startActivity 和startActivityForResult两种, 返回的话用finish方法,如下示例 MyActivity.this.finish();那你返回按钮使 ...
- RHEL7 -- 使用team替换bonding实现链路聚合网卡绑定
将网卡enp0s8.enp0s9进行链路绑定 安装teamd包 # yum install teamd 创建一个team链接 # nmcli con add con-name team0 type t ...
- android 屏幕适配问题
转自http://blog.sina.com.cn/s/blog_74c22b210100tn3o.html 如何将一个应用程序适配在不同的手机上,虽然这不算是一个技术问题,但是对于刚刚做屏幕的开发人 ...
- tomcat在debug模式启动直接提示:弹框无法启动,无报错信息;但直接启动的话,就会有报错信息
今天运行项目,Debug模式启动Tomcat,直接弹框:无法启动(翻译,因为后来整理,所以都忘记当时的截图) 后来尝试直接start,发现不弹框了,但是console有报出错信息. 类似以下错误 20 ...
- mysql 5.7安装教程
一.mysql下载地址 https://downloads.mysql.com/archives/installer/ 说在前面的话 我为什么已经尝试和使用过同类型产品的很多MySQL版本,还要书写 ...
- es6 解构赋值 新认知/新习惯
es6 的解构赋值其实很早就学习了,但一直纠结于习惯和可读性问题,所以没有大规模使用.最近被 react调教一番之后.已经完全融入认知和习惯中去了.总结一下三个常用的技巧: 对象取值 取值并重命名 剩 ...
- 统计svn 代码提交情况
统计svn代码提交,使用工具 statsvn.jar 下载地址:http://sourceforge.net/projects/statsvn/ rem 声明一个时间变量 作为文件名 %time:~, ...
- 怎么使用 bat 使用日期时间重命名文件名
d: rename A.txt "A%date:~0,4%-%date:~5,2%-%date:~8,2%_%time:~0,2%-%time:~3,2%-%time:~6,2%_backu ...