二、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( ...
随机推荐
- TIJ读书笔记02-控制执行流程
TIJ读书笔记02-控制执行流程 TIJ读书笔记02-控制执行流程 if-else 迭代 无条件分支 switch语句 所有条件语句都是以条件表达式的真假来决定执行路径,也就是通过布尔测试结果来决 ...
- 【Flask】Flask-Sqlalchemy使用笔记
### 安装:```shellpip install flask-sqlalchemy``` ### 数据库连接:1. 跟sqlalchemy一样,定义好数据库连接字符串DB_URI.2. 将这个定义 ...
- 主攻ASP.NET.4.5.1 MVC5.0之重生:空地搭建一个包含 Ninject框架 项目
1.创建一个空白解决方案 2.添加一个类库 名称为XXX.Domain 3.添加一个ASP.MVC 名称为XXX.WebUI 4.选着空模版,勾选MVC核心引用 5.添加单元测试项目XXX.UntiT ...
- Python 时间日历类型
# 时间日历 # time模块 # 提供了处理时间和表示之间转换的功能 # 获取当前时间戳 # 概念 # 从0时区的1970年1月1日0时0分0秒, 到所给定日期时间的秒数 # 浮点数 # 获取方式 ...
- juniper ssg 常用命令
netscreen juniper ssg操作命令 2013年4月10日 命令行下取得配置信息 get config 命令行下取得相应时间设置 get clock set vrout ...
- javax.mail.MessagingException: Could not connect to SMTP host: smtp.xdf.cn
1.问题描述:关于使用Java Mail进行邮件发送,抛出Could not connect to SMTP host: xx@xxx.com, port: 25的异常可能: 当我们使用Java Ma ...
- SpringCloud Bus消息总线
在微服务架构中,通常会使用轻量级的消息代理来构建一个共用的消息主题来连接各个微服务实例,它广播的消息会被所有在注册中心的微服务实例监听和消费,也称消息总线. SpringCloud中也有对应的解决方案 ...
- 经典的MapReduce1中的失败
经典的MapReduce1中的失败在MapReduce1运行时,主要考虑三种失败的模式,运行任务失败.tasktracker失败以及jobtracker失败1. 任务运行失败首先考虑子任务失败的情况. ...
- hadoop 输出中文乱码问题
本文转载至: http://www.aboutyun.com/thread-7358-1-1.html hadoop涉及输出文本的默认输出编码统一用没有BOM的UTF-8的形式,但是对于中文的输出wi ...
- myEclipse 2014 破解教程
因为经常在不同电脑里安装配置下载myEclipse,所以干脆记录下来,一直找度娘也是很麻烦的. 此教程仅对myEclipse2014 有效. 破解工具:https://pan.baidu.com/s/ ...