发布于2020-02-24 21:53 阅读(1010) 评论(0) 点赞(16) 收藏(1)
学习python有几个月了,但是一直没有总结过,后来看别人代码的时候发现有很多知识漏洞。于是想到了记笔记这个方法,写这样的一份学习笔记是一个梳理的过程,可以让我们更好得记住那些盲点,让自己的python基础更扎实。
a = "方平文"
b = "苹"
c = "方"
print(b in a)
print(c in a)
结果是:
False
True
num = "0011"
v = int(num, base=16)
#将num基于16进制转换成数字类型(int是代表数字类型,即为整型)
print(v)
结果是:17
age = 5
r = age.bit_length()
print(r)
这结果一定是:3
test = "alex"
v = test.capitalize() #首字母大写
v1 = v.casefold() #首字母变小写
v2 = test.center(20,"*") #将test占20个空格且把test放在中间,周围用"*"补齐
v3 = test.ljust(20,"*") #将test中的内容放左边
v4 = test.rjust(20,"*") #将test中的内容放右边
v5 = test.upper() #全部变为大写
print(v)
print(v1)
print(v2)
print(v3)
print(v4)
print(v5)
结果是:
Alex
alex
alex
alex****************
****************alex
ALEX
test = ' alex '
v = test.lstrip() #去除左边的空格和\t,\n
v1 = test.rstrip() #去除右边的空格和\t,\n
v2 = test.strip() #两边都去除
v3 = 'linghaifenghai.cn'.strip('hai.cn') #去除字符串中的'hai.cn'(从右往左数)
print(v)
print(v1)
print(v2)
print(v3)
结果是:
test = "alexalexr"
v = test.count('e',2,8) #从第2个字符到第8个字符,数e在test中出现的次数
v1 = test.endswith('r') #判断是否以字符r结尾
v2 = test.startswith('r') #判断是否以字符r开始
v3 = test.find('ex',5,8) #在5<= 和 <8之间是否有ex字符
print(v)
print(v1)
print(v2)
print(v3)
结果是:
2
True
False
6
Ps:此处v3的运行结果是6,说明find()方法返回了子串’ex’在test中的索引位置,而且是从右往左数
# format可以修改{}中的值
test = "I am {name}, age {a}"
v = test.format(name='alex',a=19)
v1 = "if i {} rain, {} would call {} the course.".format('were to','we','off')
print(v)
print(v1)
结果是:
I am alex, age 19
if i were to rain, we would call off the course.
test = "utasf890_"
v = test.isalnum()
v1 = test.isalpha() #判断是否全是字母
v2 = test.isdigit() #判断是否是数字字符串
v3 = test.isprintable() #是否存在不可显示的字符,如\n,\t等转义字符
print(v)
print(v1)
print(v2)
print(v3)
结果是:
False
False
False
True
test = "if it were to rain, we would call off the course."
v = test.title() #将字符串转化成标题样式,即首字母大写
v1 = test.istitle() #判断是否是标题
v2 = v.istitle()
print(v)
print(v2)
print(v2)
结果是:
If It Were To Rain, We Would Call Off The Course.
True
True
#用某个字符将字符串拼接起来
test = "你是风儿我是沙"
v = '-'.join(test) #用'-'号将test分割
print(v)
结果是:
你-是-风-儿-我-是-沙
test = "ajfkdsahfskhsadfss"
v = test.partition('s') #用's'将test分割成多个字符串
v1 = test.split('s') #用's'尽可能多得将test分割
v2 = test.split('s',3) #匹配三次's',分割三次,切成4节子串
v3 = test[0:2] #切片,获取0<= <2的范围内的字符
print(v)
print(v1)
print(v2)
print(v3)
结果是:
(‘ajfkd’, ‘s’, ‘ahfskhsadfss’)
[‘ajfkd’, ‘ahf’, ‘kh’, ‘adf’, ‘’, ‘’]
[‘ajfkd’, ‘ahf’, ‘kh’, ‘adfss’]
aj
test = "alexalexalex"
a = len(test) #获取字符串长度(2.7版本中一个中文字符是3个字节)
v = test.replace("ex",'bbb') #将test中的'ex'都替换成'bbb'
print(a)
print(v)
结果是:
12
albbbalbbbalbbb
test = "愿这个世界与你温柔以待"
for item in test:
print(item)
break
结果只会出现一次"愿",因为第一次循环结束就break跳出了
test = input(">>>")
for item in range(0,len(test)):
print(item,test[item])
输入:love
结果是:
0 l
1 o
2 v
3 e
该方法类似于C语言中char类型数组下标的用法
作者:爸爸去挣钱我去幼儿园
链接:https://www.pythonheidong.com/blog/article/232253/fc3eba9a55d8da042cac/
来源:python黑洞网
任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任
昵称:
评论内容:(最多支持255个字符)
---无人问津也好,技不如人也罢,你都要试着安静下来,去做自己该做的事,而不是让内心的烦躁、焦虑,坏掉你本来就不多的热情和定力
Copyright © 2018-2021 python黑洞网 All Rights Reserved 版权所有,并保留所有权利。 京ICP备18063182号-1
投诉与举报,广告合作请联系vgs_info@163.com或QQ3083709327
免责声明:网站文章均由用户上传,仅供读者学习交流使用,禁止用做商业用途。若文章涉及色情,反动,侵权等违法信息,请向我们举报,一经核实我们会立即删除!