python中的字符串
来源:互联网 发布:免费买东西软件 编辑:程序博客网 时间:2024/05/23 12:18
python字符串
1. 单引号和双引号作用相同
2. 字符串格式化方法
.upper(): 把所有字母转换为大写 ‘HELLOWWORLD’
.lower(): 把所有字母转换为小写 ‘helloworld’
.capitalize():把字符串中的首字母大写,并把剩余字母转换为小写 ‘Helloworld’
.title(): 把首字母以及每个空格或者标点符号后面的字母转换为大写。其他字母转换为小写 ‘HelloWorld’
3. 数学运算与比较运算
+: 连接两个字符串
*:将原来的字符串重复五次
>>>a='hellow'
>>> a+a
'hellowhellow'
>>> a*5
'hellowhellowhellowhellowhellow'
4. 删除空格
.strip(): 删除开头和结尾的所有的空格
.rstrip(): 删除末尾的空格
.lstrip(); 删除开头的空格
函数里可以传值
>>>a=' hello word '
>>>a.strip()
'helloword'
>>>a.lstrip()
'helloword '
>>>a.rstrip()
' hello word'
>>>a="####hello world ###"
>>>a.lstrip()
'####helloworld ###'
>>>a.lstrip('#')
'hello world ###'
5. 查找和替换文本
count(): 返回一个字符串在另一个字符串中出现的次数
>>>long_text='the book is very the man'
>>>long_text.count('t')
find():语法
str.find(str,beg=0,end=len(string))
str–指定检索的字符串
beg—开始索引,默认为0
end—结束索引,默认为字符串的长度
返回值:如果包含子字符串返回开始的索引值,否则返回-1
>>> a
'####hello world ###'
>>> a.find('h',3,9)
replace():synax
str.repalce(old,new,max)
>>> a
'####hello world ###'
>>> a.replace('hello','HELLO')
'####HELLO world ###'
字符串中的 old(旧字符串)替换成 new(新字符串),如果指定第三个参数max,则替换不超过 max 次。
- python中的字符串
- Python中的字符串处理
- Python中的字符串
- Python中的字符串
- python 中的字符串操作
- python 中的字符串连接
- python 中的字符串处理
- python 中的字符串操作
- python中的字符串切片
- Python中的字符串
- python 中的字符串操作
- Python 中的字符串编码
- Python 中的字符串编码
- Python中的原始字符串
- Python中的字符串
- python中的字符串切片
- python中的字符串
- python中的字符串
- [Paper Reading] The Implementation of Lua 5.0
- 通过define _CRTDBG_MAP_ALLOC宏来检测windows上的code是否有内存泄露
- 架构笔记
- TCP窗口控制的作用
- 视图
- python中的字符串
- Android系统启动过程---uboot,kernel,android
- Openstack学习笔记(八)-使用Devstack自动化部署openstack重启脚本
- bash有意思的记录
- poj 3264 Balanced Lineup(ST)
- 【操作系统】操作系统基础
- 写给大家看的Web设计书
- edit distance
- (水)POJ-1426 BFS,DFS