二、python沉淀之路~~字符串属性(str)
1、capitalize的用法:即将输出字符串首字母大写
test = "heLLo"
v = test.capitalize()
print(v)
结果:Hello。
2、casefold和lower的用法以及区别
test = "heLLo"
v1 = test.casefold()
print(v1)
v2 = test.lower()
print(v2)
结果:hello,hello。结果相同,但是适用范围不一样。casefold可以识别世界上大部分国家的 语言转换,而 lower只适用于英语
3、center的用法
test = "heLLo"
v3 = test.center(20)
print(v3)
v4 = test.center(20,"*")
print(v4)
结果:
heLLo
*******heLLo********
输出设置宽度,并且将字符串放置中间,而且两边可以设置填充物。
4、count、endswith,startswith三个的用法
test = "helloworldhello"
v = test.count("l") #统计 l 出现的次数
v1 = test.count("l",3,5) #在3到5的范围内统计“l”出现的次数
print(v)
v3 = test.endswith("o") #判断字符串是否已"l"结尾的,是则返回True,否则返回False
v4 = test.endswith("w",2,7)#在2到7的范围内判断是否以"w"结尾
print(v3)
print(v4)
5
True
False
startswith 的用法与endswith一样
5、find 和index的用法以及区别
test = "helloworldhello"
v = test.find("w")
v1 = test.find("l")
print(v,v1)
v2 = test.find("l",6,10)
print(v2)
#v3 = test.index("l")
5 2
8
find和index都是找某个子字符串的位置,而且可以指定范围的寻找。区别在于find找不到时返回-1,index找不到时会报错
6、format和format_map格式化字符串的用法
test = 'i an {name},age {a}'
test1 = 'i am {0},age{1}'
v = test.format(name="zhongguo",a='') #修改内容
v1 = test1.format("xiaoming",'') #自动匹配位置
print(v)
print(v1)
v2 = test.format_map({"name":'zhong','a':18})#format_map的用法就是{}里面加字典形式的内容
print(v2)
i an zhongguo,age 18
i am xiaoming,age18
i an zhong,age 18
7、isalnum()判断一串字符知否只含有字母和数字
test = 'userna1244'
test1 = 'userna1244_'
v = test.isalnum()
print(v)
v1 = test1.isalnum()
print(v1)
True
False
当被判断字符串内只有字符和数字时 ,返回True,如果哦还是有其他的符号,返回False
8、expandtabs的用法
test = 'use\trn\ta12\t44\nuse\trn\ta12\t44\nuse\trn\ta12\t44\nuse\trn\ta12\t44\n'#每段字符串是10个,只要遇到\t就空格填充
v = test.expandtabs(10)
print(v)
use rn a12 44
use rn a12 44
use rn a12 44
use rn a12 44
expandtabs和\t配合的使用,就会出现以上的输出效果
9、isalpha的用法
test = "helloworldhello"
test1 = "helloworldhello中国"
test2 = "helloworldhello中国124"
v = test.isalpha()
print(v)
v1 = test1.isalpha()
print(v1)
v2 = test2.isalpha()
print(v2)
True
True
False
判断一串字符里面是否只有字母和汉字,是则返回True,否则返回False
10、isdentifier,isdigit,dinumeric三者的用法和区别
test = ''
test1 = '1234②'
test2 = '1234②二'
v = test.isdecimal()
v0 = test1.isdecimal()
v00 = test2.isdecimal()
print(v)
print(v0)
print(v00)
v1 = test1.isdigit()
v11 = test2.isdigit()
print(v1)
print(v11)
v2 = test2.isnumeric()
print(v2)
True
False
False
True
False
True
isdecimal只能判断是否是单纯的数字、isdigit不仅可以判断纯数字,还可以判断特殊序号;isnumeric除了可以判断纯数字和特殊序号外,还可以识别中文数字
11、isidentifier的用法
test = "helloworld"
test1 = "123helloworld"
test2 = "_helloworld"
test3 = "中国helloworld"
test4 = "*中国helloworld"
v = test.isidentifier()
print(v)
v1 = test1.isidentifier()
print(v1)
v2 = test2.isidentifier()
print(v2)
v3 = test3.isidentifier()
print(v3)
v4 = test4.isidentifier()
print(v4)
True
False
True
True
False
判断是否是以字母、下划线、中文开头的,是则返回True,否则返回False
12、isprintable的用法
test = 'aasdjl'
test1 = 'aas\tdjl'
test2 = 'aas\ndjl'
v = test.isprintable()
print(v)
v1 = test1.isprintable()
print(v1)
v2 = test2.isprintable()
print(v2)
True
False
False
判断是否存在不可显示的字符,没有则返回True ,有则返回False
13、isspace的用法
test = ''
test1 = ' '
test2 = 'abc abc '
v = test.isspace()
print(v)
v1 = test1.isspace()
print(v1)
v2 = test2.isspace()
print(v2)
False
True
False
判断字符串是否是空的,是则返回True,否则返回False
14、istitle和title的用法
test = 'Hello world good morning'
test0 = 'Hello World Good Morning'
v = test.istitle()
print(v)
v0 = test0.istitle()
print(v0)
v1 = test.title()
print(v1)
False
True
Hello World Good Morning
istitle判断是否是以标题形式书写的字符串,是则返回True,否则返回False,title是将不是标题形式的字符串转换成标题形式的字符串
15、join 用法【重点】
test = 'Hello world good morning'
test1 = 'Hello'
test2 = 'Hello'
test3 = 'Hello'
v = ' '.join(test)
print(v)
v1 = ' '.join(test1)
print(v1)
v2 = ''.join(test2)
print(v2)
v3 = '*'.join(test3)
print(v3)
H e l l o w o r l d g o o d m o r n i n g
H e l l o
Hello
H*e*l*l*o
格式化字符串,即占位符
16、ljust rjust center zfill 四个的用法,对比理解
test = 'Hello'
v = test.ljust(20,'*')
print(v)
v1 = test.rjust(20,'*')
print(v1)
v2 = test.center(20,'*')
print(v2)
v4 = test.zfill(20)
print(v4)
Hello***************
***************Hello
*******Hello********
000000000000000Hello
都可以设置长度,前三个都可以指定填充元素;最后一个不能填充元素,只能按照他默认的。
17、islower lower isupper upper 四个的用法
test = 'HeLLo'
v1 = test.islower()
print(v1)
v2 = test.lower()
print(v2)
print(test)
v3 = test.isupper()
print(v3)
v4 = test.upper()
print(v4)
False
hello
HeLLo
False
HELLO
18、lstrip rstrip strip的用法
test = ' HeLLo '
v = test.lstrip()
print(v)
v1 = test.rstrip()
print(v1)
v2 = test.strip()
print(v2)
HeLLo
HeLLo
HeLLo
分别是去除右边、左边、两边的的空白
补:
test = 'HeLLoHeLLo'
v = test.lstrip('HeL')
print(v)
v1 = test.rstrip('Lo')
print(v1)
v2 = test.strip('He')
print(v2)
oHeLLo
HeLLoHe
LLoHeLLo
19、maketrans translate 的用法
test = 'hello;world;good'
new_test = test.translate(v1)
v1 = str.maketrans('good','')
print(v1)
print(new_test)
{103: 49, 111: 51, 100: 52}
hell3;w3rl4;1334
对应关系替换
20、partition rpartiton splite rsplite splitelines 的用法
test = 'helloworld'
v = test.partition('w')
print(v)
v1 = test.rpartition('l')
print(v1)
v2 = test.split('o')
print(v2)
v3 = test.rsplit('o')
print(v3)
test1 = 'hel\nlowor\nld'
v4 = test1.splitlines()
print(v4)
('hello', 'w', 'orld')
('hellowor', 'l', 'd')
['hell', 'w', 'rld']
['hell', 'w', 'rld']
['hel', 'lowor', 'ld']
partition切出来只有三个元素,split切出来会把指定元素消除。splitlines 是以\n为切割标志
21、swapcase 的用法
test = 'HelloWorlD'
v= test.swapcase()
print(v)
hELLOwORLd
小写转大写,大写转写
总结:
join split find strip upper lower 这六个必须掌握
字符串可以索引,切片,
也可以用len测试字符串的长度,还可以在for 循环里面被遍历。
二、python沉淀之路~~字符串属性(str)的更多相关文章
- python沉淀之路~~整型的属性
python的基础知识: 基本数据类型:int str list tuple dict bool 一.整型的属性功能 1.工厂方法将字符串转换成整型 a = " b = ...
- 六、python沉淀之路--int str list tuple dict 重点总结
一.数字int(..)二.字符串replace/find/join/strip/startswith/split/upper/lower/formattempalte = "i am {na ...
- 十二、python沉淀之路--内置函数
1.abs函数,求绝对值. a = abs(-3) print(a) 返回:3 2.all函数:判断是否是可迭代对象. 官方解释:Return True if bool(x) is True for ...
- 十六、python沉淀之路--迭代器
一.迭代器 1.什么是迭代器协议:对象必须提供一个next方法,执行该方法要返回迭代中的下一项,要么就引起一个StopIteration异常,以终止迭代(只能往后走,不能往前走). 2.可迭代对象:实 ...
- 十五、python沉淀之路--eval()的用法
一.eval函数 python eval() 函数的功能:将字符串str当成有效的表达式来求值并返回计算结果. 语法:eval(source[, globals[, locals]]) -> v ...
- 四、python沉淀之路--元组
一.元组基本属性 1.元组不能被修改,不能被增加.不能被删除 2.两个属性 tu.count(22) #获取指定元素在元组中出现的次数tu.index(22) #获取指定元素的缩 ...
- 七、python沉淀之路--集合
一. 1.字符串转集合 s = 'hello' se = set(s) print(se) {'e', 'o', 'h', 'l'} 2.列表转集合 l1 = ['hello','python','n ...
- 五、python沉淀之路--字典
一. 1.根据序列,创建字典,并指定统一的值 v = dict.fromkeys(["],222) print(v) {': 222} 2.根据key 获取值,key不存在时,报错:get方 ...
- 三、python沉淀之路--列表(list)
一.列表提供的一些方法 1.append():在原值最后追加 li = [11,22,33,44,55,] li.append(99) print(li) li.append('中国') print( ...
随机推荐
- iOS KVC 和 KVO 的学习
KVC (NSKey Value Coding) :键值编码 KVO (Key Value Observing) :键值监听 前言:我曾经用过 监听 一个音频何时结束 监听视频播放 状态等 用了这种 ...
- 更改计算机名称,影响TFS之前映射的工作区 使用。
今天把自己电脑的计算机名称改了,打开vs2012的时候,就提示以下的错误: ---------------------------Microsoft Visual Studio------------ ...
- 树莓派打造对话机器人 Python(转)
工具列表 1. **树莓派**(型号不要求,本人使用的是3B) 2. **usb麦克风**(某宝有卖,我就不打广告了) 用来录音 3. **音响或者喇叭**(某宝也有卖) 用来播放 以上就是需要的工具 ...
- 个人对于css sprite的一点点见解
css sprite即CSS雪碧图又称CSS精灵.它存在的一个主要作用就是:减少了网页的http请求次数,从而大大的提高了页面的性能,节省时间和带宽. 例如 这样算下来.CSS sprite真的是个很 ...
- 【Flask】ORM 关系一对一
### 一对一的关系:在sqlalchemy中,如果想要将两个模型映射成一对一的关系,那么应该在父模型中,指定引用的时候,要传递一个`uselist=False`这个参数进去.就是告诉父模型,以后引用 ...
- Django详解之四、cookie和session
一.使用背景 思路 简单的后台管理:对人员的管理 1. 登录注册 2. 老师 班级管理 学院管理 3. 增删改查 开发: 1. 定义数据库表结构 a) 表结构关系 i. class classes(m ...
- iOS_XML与JSON解析
XML与JSON简介 XML 可扩展标记语言 用于标记电子文件使其具有结构性的标记语言,可以用来标记数据.定义数据类型,是一种允许用户对自己的标记语言进行定义的源语言 易读性高,编码手写难度小,数据量 ...
- 在.h和.cpp中包含头文件的区别
1.在.h中包含头文件,是为了声明一系列这个头文件的变量等,可能会产生重复包含的问题: 2.在.cpp中包含头文件只是为了实现这个头文件或者使用其中的方法,不会有重复包含的问题,所以尽量在源文件中包含 ...
- java Web 文件上传
注意:请求实体过大的问题,请修改Nginx服务器的大小(百度参考413 Request Entity Too Large 的解决方法)jsp:<input type="file&quo ...
- MongoDB快速入门(五)- Where子句
RDBMS Where子句等效于MongoDB 查询文档在一些条件的基础上,可以使用下面的操作 操作 语法 示例 RDBMS等效语句 Equality {<key>:<value&g ...