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)的更多相关文章

  1. python沉淀之路~~整型的属性

    python的基础知识: 基本数据类型:int   str   list   tuple   dict   bool 一.整型的属性功能 1.工厂方法将字符串转换成整型 a = " b = ...

  2. 六、python沉淀之路--int str list tuple dict 重点总结

    一.数字int(..)二.字符串replace/find/join/strip/startswith/split/upper/lower/formattempalte = "i am {na ...

  3. 十二、python沉淀之路--内置函数

    1.abs函数,求绝对值. a = abs(-3) print(a) 返回:3 2.all函数:判断是否是可迭代对象. 官方解释:Return True if bool(x) is True for ...

  4. 十六、python沉淀之路--迭代器

    一.迭代器 1.什么是迭代器协议:对象必须提供一个next方法,执行该方法要返回迭代中的下一项,要么就引起一个StopIteration异常,以终止迭代(只能往后走,不能往前走). 2.可迭代对象:实 ...

  5. 十五、python沉淀之路--eval()的用法

    一.eval函数 python eval() 函数的功能:将字符串str当成有效的表达式来求值并返回计算结果. 语法:eval(source[, globals[, locals]]) -> v ...

  6. 四、python沉淀之路--元组

    一.元组基本属性 1.元组不能被修改,不能被增加.不能被删除 2.两个属性 tu.count(22)       #获取指定元素在元组中出现的次数tu.index(22)      #获取指定元素的缩 ...

  7. 七、python沉淀之路--集合

    一. 1.字符串转集合 s = 'hello' se = set(s) print(se) {'e', 'o', 'h', 'l'} 2.列表转集合 l1 = ['hello','python','n ...

  8. 五、python沉淀之路--字典

    一. 1.根据序列,创建字典,并指定统一的值 v = dict.fromkeys(["],222) print(v) {': 222} 2.根据key 获取值,key不存在时,报错:get方 ...

  9. 三、python沉淀之路--列表(list)

    一.列表提供的一些方法 1.append():在原值最后追加 li = [11,22,33,44,55,] li.append(99) print(li) li.append('中国') print( ...

随机推荐

  1. iOS AVPlayer 学习

    1 .使用环境: 在实际开发过程中 有需要展示流媒体的模块 ,需求非常简单 :播放 和 暂停 ,其实这个时候有很多选择 ,可以选择 MPMoviePlayerController(MediaPlaye ...

  2. 所有文本的 attributes 枚举,NSAttributedString

    // Predefined character attributes for text. If the key is not in the dictionary, then use the defau ...

  3. 单口双线PC连接转换器 手机电脑耳机转接线

    看着标题是不是很绕, 其实这个需求我相信不少人都有, 只是可能很少会想到. 手机换了一个又一个, 佩戴的耳机同样是一个又一个, 最别扭的是, 用手机的时候往往不用耳机, 不少童鞋都会选择把手机的耳机放 ...

  4. $.queue() 与 $.dequeue() -- 队列

    JQuery 运用队列为动画模块服务,但好像它应该有更多用处,我觉得的,那试试就知道咯. 简单的来讲,它就是形成队列和出列, 也就因此可以进行很有规律的回调和延时了呀(暂停感觉有难度),当然这就是后面 ...

  5. HDU 3449 Consumer

    这是一道依赖背包问题.背包问题通常的解法都是由0/1背包拓展过来的,这道也不例外.我最初想到的做法是,由于有依赖关系,先对附件做个DP,得到1-w的附件背包结果f[i]表示i花费得到的最大收益,然后把 ...

  6. php数组函数-array_flip()

    array_flip()函数返回一个反转后的数组,如果同一个值出现多次,则最 后一个键名作为它的值,所有其他的键名将丢失. 如果原数组中的值得数据类型不是字符串或整数,函数将报错. array_fli ...

  7. PHP实现链式操作

    什么是链式操作 我们经常会在一些应用框架中看到如下代码: $db = new Database; $db->where('cid = 9')->order('aid desc')-> ...

  8. 0x5C 计数类DP

    cf 559C 考虑到黑色的格子很少,那么我把(1,1)变成黑色,然后按每个黑色格子接近终点的程度排序,计算黑色格子不经过另一个黑色格子到达终点的方案,对于当前的格子,要减去在它右下角的所有方案数(注 ...

  9. poj 2762 Going from u to v or from v to u?【强连通分量缩点+拓扑排序】

    Going from u to v or from v to u? Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 15812 ...

  10. python去掉行尾的换行符

    python去掉行尾的换行符 mystring.strip().replace(' ', '').replace('\n', '').replace('\t', '').replace('\r', ' ...