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 ...
随机推荐
- 【php】如何配置自主域名腾讯企业邮箱
腾讯企业邮配置 protocal ssl smtp port 465 host smtp.exmail.qq.com user email account passwd email passwd
- Computer HDU - 2196
Computer HDU - 2196 A school bought the first computer some time ago(so this computer's id is 1). Du ...
- Codeforces Round #462 (Div. 2) C. A Twisty Movement
C. A Twisty Movement time limit per test1 second memory limit per test256 megabytes Problem Descript ...
- Redis实现之事件
事件 Redis服务器是一个事件驱动程序,服务器需要处理以下两类事情: 文件事件(file event):Redis服务器通过套接字与客户端(或者其他Redis服务器)进行连接,而文件事件就是服务器对 ...
- Python协程详解(一)
yield有两个意思,一个是生产,一个是退让,对于Python生成器的yield来说,这两个含义都成立.yield这个关键字,既可以在生成器中产生一个值,传输给调用方,同时也可以从调用方那获取一个值, ...
- cf965c Greedy Arkady
呸,大傻逼题,我更傻逼ref #include <iostream> using namespace std; typedef long long ll; ll n, k, m, d, a ...
- 剖析微软Hyper-V的最佳部署方式
剖析微软Hyper-V的最佳部署方式 2014-04-24 10:53 布加迪编译 51CTO.com 字号:T | T 微软Hyper-V有两种不同的版本.既可以安装到Windows Server的 ...
- C 语言 习题 1-10
练习 1-10 编写一个将输入复制到输出的程序,并将其中的制表符替换为\t,把回退符替换为\b,把反斜杠替按为\\.这样可以将制表符和回退符以可见的方式显示出来. #include<stdio. ...
- Windows核心编程小结2
这一节看看内存管理相关的信息 首先看看虚拟内存 虚拟地址空间 32位系统 --- 4GB = 232 64 位系统 ---- 16EB = 264 虚拟内存表 当一个应用程序从硬盘加载到RAM时, ...
- 将数据缓存到sessionStorage中
//获取侧边栏 if (sessionStorage.getItem(`${env}${empId}leftMenu`)) { const leftMenu = JSON.parse(sessionS ...