python中字符串,列表,元祖和字典的常用方法
-------------------------------center------------------
让内容居中,两边用*填充
temp="erdongchen"
ret=temp.center(20,"*")
print(ret)
--------------------------------capitalize--------------
字符串的首写字母变为大写
temp="erdongchen"
ret=temp.capitalize();
print(ret)
--------------------------------count---------------------
统计字符串里面有多少个e
temp="erdongchen"
ret=temp.count("e")
print(ret)
从字符串的第0个字符开始,到第三个字符。4的意思是小于3
temp="erdongchen"
ret=temp.count("e",0,4)
print(ret)
------------------------------endwith----------------------
以什么字符结尾
temp="erdongchen"
ret=temp.endswith("n")
print(ret)
以什么字符结尾,从字符串的第0个字符开始,到字符串的第3个字符。3是小于3的意思。就是0 1 2
temp="erdongchen"
ret=temp.endswith("d",0,3)
print(ret)
-------------------------------expandtabs-------------------------
\t是tab的意思,expandtabs默认是将tab空格形成8个空格
temp="erdong\tchen"
print(temp.expandtabs())
\t是tab的意思,expandtabs默认是将tab空格形成8个空格,20表示20个空格,不是默认的8个空格了
temp="erdong\tchen"
print(temp.expandtabs(20))
------------------------------find-------------------------------
find会查找d这个字符在字符串里的第几个位置。这个find只会查找第一个d所在的位置
temp="erdongchen"
ret=temp.find("d")
print(ret)
------------------------------format-------------------------------
{0} {1}是占位符来的,必须从{0} {1}{2}这样下去。format里的12345对应{0} 435678对应{1}
temp="hello {0} word {1}"
ret=temp.format(12345,435678)
print(ret)
-----------------------------join--------------------------------
将列表的内容链接在一起,*号表示每个字符串之间用*进行间隔
list=['me','want','to','love','you']
ret="*".join(list)
print(ret)
----------------------------strip------------------------------
name=" chenzhiyang "
ret=name.lstrip() #去掉字符串左边的空格
ret2=name.rstrip()#去掉字符串右边的空格
ret3=name.strip()#去掉字符串左边和右边的空格
print(ret)
print(ret2)
print(ret3)
---------------------------partition---------------------------------
#根据is为分割点,形成('chenzhiyang ', 'is', ' a shuage')三个字符串
name="chenzhiyang is a shuage"
ret=name.partition("is")
print(ret)
-----------------------------replace------------------------------
#replace是替代的功能,但是只能替代一个
#结果 shuage zhi yang
name="chen zhi yang"
ret=name.replace("chen","shuage")
print(ret)
---------------------------字符串的索引------------------------
#字符串有自己的索引,name[0]表示c ,name[1]表示h
name="chenzhiyang"
print(name[0])
print(name[1])
print(name[2])
print(name[3])
-----------------------------字符串的长度---------------------------
#len(name)表示求出字符串的长度
name="chenzhiyang"
print(len(name))
print(name[4])
--------------------------取字符串的某些连续字符------------------
#输出字符串的第0个和第1个字符,2的意思是小于2
#结果:ch
name="chenzhiyang"
print(name[0:2])
------------------------for与字符串的关系------------------------
name="chenzhiyang"
for i in name:
print(i)
---------------------------列表的切片-------------------------------
#列表也有索引和切片
list=['chen','zhi','yang']
print(list[0:2])
print(len(list))
-------------------------列表增加数据----------------------------------
#列表通过append来添加字符串
list=['chen','zhi','yang']
list.append('is')
print(list)
-------------------------count------------------------------------
#统计列表和字符串里有多少个要查询的字符串和字符
list=['chen','zhi','yang']
list.append('is')
list.append('is')
st="aaaaaaaaa"
print(list.count('is'))
print(st.count('a'))
----------------------------------将一个列表添加到另外一个列表里----------
#统计列表和字符串里有多少个要查询的字符串和字符
list=['chen','zhi','yang']
chen=['1','2','3']
list.extend(chen)
print(list)
------------------------------------index--------------------------
#查看一个字符串在列表里的位置,只查询第一个相对应的位置
list=['chen','zhi','yang']
print(list.index("chen",0,3))
----------------------------------insert---------------------
#往列表的第0位插入一个me的字符串
list=['chen','zhi','yang']
list.insert(0,'me')
print(list)
-----------------------------------pop--------------------------
#这个表示去掉列表最后面的一个字符串
list=['chen','zhi','yang']
list.pop()
print(list)
-----------------------------------remove--------------------------
#remove表示去除列表里指定的字符串,但是每一次执行只去除一个
list=['chen','zhi','yang']
list.remove('chen')
print(list)
-----------------------------------del--------------------------------
#通过指定索引删除列表里指定的字符串
list=['chen','zhi','yang']
#del list[0]
del list[0:2]
print(list)
-----------------------------------tuple 元祖----------------------------
#元祖跟列表一样,但是元祖的值是不可修改的,可以查询,比如del list[0]是不可以得
list=('chen','zhi','yang')
#print(list)
#print(list[0])
#print(list[0:2])
#print(len(list))
#print(type(list))
------------------------------------字典------------------------------
#dict字典获取值的方法,通过索引来获取值
user_info= {
"name":"chen",
"age":18,
"gender":"M"
}
print(user_info["age"])
print(user_info["name"])
#dict字典获取值的方法
user_info= {
"name":"chen",
"age":18,
"gender":"M"
}
#通过循环获取到的是字典的键,而不是值
for i in user_info:
print(i)
#获取字典里的值
for i in user_info.values():
print(i)
#获取字典里的键
for i in user_info.keys():
print(i)
#获取字典里面的键值对,key的值给k,vaule的值给v
for k,v in user_info.items():
print(k,v)
--------------------------update---------------------------
将字典test加入到user_info里面
user_info={
"name":"chen",
"age":"18",
"man":"M"
}
test={
"t1":"12",
"t2":"23456"
}
user_info.update(test)
print(user_info)
----------------------------del------------------------------
删除字典里面的一个键值对
user_info={
"name":"chen",
"age":"18",
"man":"M"
}
del user_info["name"]
---------------------------enumerate--------------------------
输出列表的同时,输出一个序号给n: 结果为
0 1234
1 3456
2 345678
st=["1234","3456","345678"]
for n,v in enumerate(st):
print(n,v)
print(user_info)
---------------------------range---------------------------
#输出1到9,这里的10是小于10
for i in range(1,10):
print(i)
#输出1,3,5,7,9 2的意思是隔2的意思
for i in range(1,10,2):
print(i)
#这个是倒序过来的 1是表示大于1,不是大于且等于1
for i in range(10,1,-1):
print(i)