python3字符串操作总结
- 字符串截取
>>>s = 'hello'
>>>s[0:3]
'he'
>>>s[:] #截取全部字符
'hello'
- 消除空格及特殊符号
s.strip() #消除字符串s左右两边的空白字符(包括'\t','\n','\r','') s.strip('') #消除字符串s左右两边的特殊字符(如'0'),字符串中间的'0'不会删除
例如:
>>>s = '000hello00world000'
>>>s.strip('')
'hello00world' s.strip('')等价于s.strip('')
例如:
>>>s = '12hello21'
>>>s.strip('')
'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 ———— 大写变小写,小写变大写 例子:
>>>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 = '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字符串操作总结的更多相关文章
- python3字符串操作
python3字符串操作 x = 'abc' y = 'defgh' print(x + y) #x+y print(x * ) #x*n print(x[]) #x[i] print(y[:-]) ...
- [No000078]Python3 字符串操作
#!/usr/bin/env python3 # -*- coding: utf-8 -*- '''Python 字符串操作 string替换.删除.截取.复制.连接.比较.查找.包含.大小写转换.分 ...
- python3 字符串操作相关函数
整理自python基础|菜鸟教程 感谢菜鸟教程提供的优质资源! 1.capitalize() 将字符串的第一个字符转换为大写 实例 以下实例展示了capitalize()方法的实例: #!/usr/b ...
- Python3 字符串操作
截掉指定字符串 # 截掉指定字符串 string.strip("what you want to delete") #截掉字符串左边的空格 string.lstrip() #截掉字 ...
- python3.4学习笔记(十五) 字符串操作(string替换、删除、截取、复制、连接、比较、查找、包含、大小写转换、分割等)
python3.4学习笔记(十五) 字符串操作(string替换.删除.截取.复制.连接.比较.查找.包含.大小写转换.分割等) python print 不换行(在后面加上,end=''),prin ...
- Python中的字符串操作总结(Python3.6.1版本)
Python中的字符串操作(Python3.6.1版本) (1)切片操作: str1="hello world!" str1[1:3] <=> 'el'(左闭右开:即是 ...
- Python3基础(2)模块、数据类型及运算、进制、列表、元组、字符串操作、字典
---------------个人学习笔记--------------- ----------------本文作者吴疆-------------- ------点击此处链接至博客园原文------ 1 ...
- Python3学习之路~2.3 字符串操作
字符串操作 特性:不可修改 name="my \tname is alex" print(name.capitalize()) #首字母变大写 print('Alex LI'.ca ...
- python3.0 day02 列表、元组 、字典、字符串操作
1.列表.元组操作 列表是我们最以后最常用的数据类型之一,通过列表可以对数据实现最方便的存储.修改等操作,类似于其他语言中的数组. 定义列表 names = ['Lioa',"Tenglan ...
随机推荐
- JZOJ 5344. 摘果子
Description Input Output Sample Input 7 9 39 6 13 2 22 6 7 4 -19 5 28 6 -17 1 2 1 3 2 4 1 5 4 6 2 7 ...
- 学习pytho第l六天 常用字符串用法
name='my name is dream' print(name.capitalize())#首字母大写 print(name.count(‘’a‘’))#判断字符串里有多少个a print(na ...
- Codeforces Round #461 (Div. 2) B. Magic Forest
B. Magic Forest time limit per test 1 second memory limit per test 256 megabytes Problem Description ...
- 二分法:CF371C-Hamburgers(二分法+字符串的处理)
Hamburgers Time Limit:1000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u Desc ...
- 成为Java高手的25个学习要点
成为Java高手的25个学习要点 想成为Java大牛吗?不妨来学习这25个要点. 1. 你需要精通面向对象分析与设计(OOA/OOD).涉及模式(GOF,J2EEDP)以及综合模式.你应该了解UML, ...
- Excel动画教程50例(一)
Excel动画教程50例(一) 1.自动筛选 2.在Excel中字符替换 3.在Excel中冻结行列标题 4.在Excel中为导入外部数据 5.在Excel中行列快速转换 6.共享Excel工作簿 7 ...
- python - 接口自动化测试 - TestLogin - 登录接口测试用例
# -*- coding:utf-8 -*- ''' @project: ApiAutoTest @author: Jimmy @file: test_login.py @ide: PyCharm C ...
- Hadoop架构的初略总结(1)
Hadoop架构的初略总结(1) Hadoop是一个开源的分布式系统基础架构,此架构可以帮助用户可以在不了解分布式底层细节的情况下开发分布式程序. 首先我们要理清楚几个问题. 1.我们为什么需要Had ...
- ACM-ICPC 2018 焦作赛区网络预赛
这场打得还是比较爽的,但是队友差一点就再过一题,还是难受啊. 每天都有新的难过 A. Magic Mirror Jessie has a magic mirror. Every morning she ...
- 初学Linux 命令
查看ip:ifconfig 切换用户:us root(root为用户名) 显示当前目录:pwd 列出当前目录下所有文件:ls 进入某个目录 :cd 创建一个文件夹:mkdir 创建多个目录(当没有该父 ...