0基础python笔记str/list/tuple/dict

——————————str(字符串)————————

capitalize

test =  'asdasdwqd'
v = test.capitalize()#首字母大写
print(v)
结果:Asdasdwqd

 casefold / lower / upper / swapcase / title

test =  'ASDDWASD WasdafqwASD'
v = test.casefold() #所有字母都变成小写,casefold,对 Unicode 的时候用 casefold
print(v)
v1 = test.lower() #所有字母都变成小写,lower() 只对 ASCII 也就是 'A-Z'有效
print(v1)
v2 = test.upper() #所有字母都变成小写
print(v2)
v3 = test.swapcase() #所有大写换成小写,小写换成大写
print(v3)
v4 = test.title() #首字母大写
print(v4)
结果:
    asddwasd wasdafqwasd
    asddwasd wasdafqwasd
    ASDDWASD WASDAFQWASD
    asddwasd wASDAFQWasd
    Asddwasd Wasdafqwasd

center /  ljust / rjust

test =  'wASD'
v = test.center(20,'*') #共打印20个字符 不够的用*号从两边向中心补全
print(v)
test = "qiqiqiq"
v = test.ljust(20,"*") #共打印20个字符 不够从右用*号补全
print(v)
v2 = test.rjust(20,"*")#共打印20个字符 不够从左用*号补全
print(v2)
结果:
********wASD********
qiqiqiq*************
*************qiqiqiq

 count

test =  'wsdadwdadasdasdasdasdASD'
v = test.count('a',3,10) #统计字符串里某个字符出现的次数。可选参数为在字符串搜索的开始与结束位置
print(v)
结果:3

endswith / startswith

test = "alex"
v = test.endswith('ex') # 以什么什么结尾
print(v)
v = test.startswith('ex') # 以什么什么开始
print(v)
结果:True
     False

 expandtabs

test = "ale\tx\nale\tx\nale\tx\n"
v = test.expandtabs(20)#字符串中有\t 那么打印时就会把\t转换成20空格
print(v)
结果:
    ale                 x
    ale                 x
    ale                 x        

find / rfind

test = "my name is "
v = test.find('m') # 从下标0开始,查找在字符串里第一个出现的子串,返回结果:0
print(v)
v = test.find('m',1) # 从下标1开始,查找在字符串里第一个出现的子串:返回结果5
print(v)
v = test.find('z') # 查找不到返回-1
print(v)
v1 = test.rfind('m') #从右边开始查找,返回结果:5
print(v1)
v1 = test.rfind('m',5) #从右边第五个下标开始查找,返回结果:5
print(v1)
结果:
    0
    5
    -1
    5
    5

format / format_map

test = "my name is {name} , is ago {year}"
print(test)
v = test.format(name='qitian',year=23)#格式化,将一个字符串中的占位符替换为指定的值
print(v)

test = 'i am {0}, age {1}'
print(test)
v= test.format('qitian',19)#格式化,将一个字符串中的占位符替换为指定的值
print(v)

test = "my name is {name} , is ago {year}"
print(test)
v = test.format_map( {'name': 'qitian', 'year': '23'} ) # 格式化,传入的值 
print(v)
结果:
my name is {name} , is ago {year}
my name is qitian , is ago 23
i am {0}, age {1}
i am qitian, age 19
my name is {name} , is ago {year}
my name is qitian , is ago 23

index / rindex

test = 'asdasdasdasdwq'
v = test.index('d',1,4) #查找下标 若不存在,找不到,报错,可以定义起始位置和结束位置 从下表1 开始查找d这个字幕,结束位置下表4
print(v)
v = test.rindex('d',1,6) #返回子字符串 d 在字符串test中最后出现的位置
print(v)
结果:
    2
    5

join / strip / rstrip / lstrip

test = 'aaaasdasdasdasdwqaaa'
print(test)
v = list(test) #转换成列表
print(v)
v2 = "".join(test) #转换成字符串
print(type(v2)) #查看类型
print(v2)
v3 = v2.strip("a") #去除两段字符a 默认是空格
print(v3)
v4 = v2.rstrip("a") #去掉右边
print(v4)
v5 = v2.lstrip("a") #去掉左边
print(v5)
结果:
aaaasdasdasdasdwqaaa
['a', 'a', 'a', 'a', 's', 'd', 'a', 's', 'd', 'a', 's', 'd', 'a', 's', 'd', 'w', 'q', 'a', 'a', 'a']
<class 'str'>
aaaasdasdasdasdwqaaa
sdasdasdasdwq
aaaasdasdasdasdwq
sdasdasdasdwqaaa

maketrans / translate

v = str.maketrans("abcdefj","1234567") #生成一个转换表, 要一一对应.
test = "bceja"
print(test.translate(v)) #用创建的映射表,test转换字符串
结果:
    23571

partition / rpartition

test = "1a2a3a4a5a6a7a8a9a"
v = test.partition("6a")  # 指定的分隔符。6a
print(v) #返回一个3元的元组,第一个为分隔符左边的子串,第二个为分隔符本身,第三个为分隔符右边的子串。
print(v[1])

v1 = test.rpartition("8a")
print(v1)
print(v1[1])
结果:
    ('1a2a3a4a5a', '6a', '7a8a9a')
    6a
    ('1a2a3a4a5a6a7a', '8a', '9a')
    8a

replace

test = "abcdefgabcdefg"
v = test.replace('abcd','1234',2) #替换,将abcd替换成12234,参数2,是替换的量 这里面说就替换二个
print(v)
结果:
1234efg1234efg

split / rsplit 

test = "asdawdqdasdqwxsxqwd"
v = test.split("d")
print(v)
test = "asdawdqdasdqwxsxqwd"
v = test.rsplit("w",1)
print(v)
#方法通过指定分隔符对字符串进行分割并返回一个列表,默认分隔符为所有空字符,包括空格、换行(\n)、制表符(\t)等。类似于 split() 方法,只不过是从字符串最后面开始分割。
sep -- 可选参数,指定的分隔符,默认为所有的空字符,包括空格、换行(\n)、制表符(\t)等。
count -- 可选参数,分割次数,默认为分隔符在字符串中出现的总次数。
结果:
    ['as', 'aw', 'q', 'as', 'qwxsxqw', '']
    ['asdawdqdasdqwxsxq', 'd']

splitlines

test = 'ab c\n\nde fg\rkl\r\n'
print(test.splitlines())
test1 = 'ab c\n\nde fg\rkl\r\n'
print(test1.splitlines(True))
#在输出结果里是否去掉换行符('\r', '\r\n', \n'),默认为 False,不包含换行符,如果为 True,则保留换行符
结果:
    ['ab c', '', 'de fg', 'kl']
    ['ab c\n', '\n', 'de fg\r', 'kl\r\n']

 

——————————str(字符串判断 True/False)————————
isalnum  字符串中是否只包含 字母和数字

test = "12aasd3"
v = test.isalnum()
print(v)
test1 = "12aasd3@$%#"
v1 = test1.isalnum()
print(v1)
结果:
    True
    False

isalpha 判断字符串是否纯英文字符

test = "asaasd"
v = test.isalpha()
print(v)
test1 = "12aasd3@$%#"
v1 = test1.isalpha()
print(v1)
结果:
    True
    False

isdigit  判断字符串是否为纯数字

test = "123123123"
v = test.isdigit()
print(v)
test1 = "12312qweqw"
v1 = test1.isdigit()
print(v1)
结果:
    True    
    False

isidentifier 判断字符串是否是字母开头

print("name".isidentifier()) 
print("1True".isidentifier())
结果:
    True
    False

 islower 判断字符串是否为小写

test = "asdasda"
v = test.islower()
print(v)
test1 = "ddwqdDSD"
v1 = test1.islower()
print(v1)
结果:
    True
    False

isnumeric 判断字符串是否只由数字组成。这种方法是只针对unicode对象。

test = "asjdhgjasd"
test1 = "12123123"
v = test.isnumeric()
print(v)
v1 = test1.isnumeric()
print(v1)
结果:
    False
    True

isprintable 是不是可以打印的 ,能打印的都是true

print("My Name Is".isprintable())

isspace  判断字符串是否为空格

test = "123123"
v = test.isspace()
print(v)
test1 = " "
v1 = test1.isspace()
print(v1)
结果:
    False
    True

istitle 判断首字母是否为大写

test = "Asdasd Asdas"
v = test.istitle()
print(v)
test1 = "Asdasd aAasdasd"
v1 = test1.istitle()
print(v1)
结果:
    True
    False

isupper 判断所有字符是否为大写

test = "asdasdasd"
v = test.isupper()
print(v)
test1 = "SADSADSA"
v1 = test1.isupper()
print(v1)
结果:
     False
     True

 

——————————list(列表)————————
append  添加

test = ['a','b','c','d','e','f','g']
v = test.append("z") #在列表末尾添加新的对象
print(test)
结果:
    ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'z']

clear 清除

test = ['a','b','c','d','e','f','g']
v = test.clear()
print(test)
结果:
    []

 copy 拷贝

test = ['a','b','c','d','e','f','g']
v = test.copy()
print(test)
print(v)
结果:
    ['a', 'b', 'c', 'd', 'e', 'f', 'g']
    ['a', 'b', 'c', 'd', 'e', 'f', 'g']

 index 查找对应的下标号

test = ['a','b','c','d','e','f','g']
v = test.index("c")
print(v)
结果:
    2

insert 插入

test = ['a','b','c','d','e','f','g']
v = test.insert(5,'z') #第一个参数待变在那个下表插入,第二参数是插入的值
print(test) 
结果:
   ['a', 'b', 'c', 'd', 'e', 'z', 'f', 'g'] 

pop 移除列表中的一个元素(默认最后一个元素)

test = ['a','b','c','d','e','f','g']
v = test.pop()
v2 = test.pop()
print(v)
print(v2)
print(test)

remove 指定列表元素删除

test = ['a','b','c','d','e','f','g']
v = test.remove("c",)
print(test)
结果:
    ['a', 'b', 'd', 'e', 'f', 'g']

 reverse 反转

test = ['a','b','c','d','e','f','g']
print(test)
v = test.reverse()
print(test)
结果:
    ['a', 'b', 'c', 'd', 'e', 'f', 'g']
    ['g', 'f', 'e', 'd', 'c', 'b', 'a']

sort 排序

test = ['a','b','f','c','e','d','g','1','7','3','6','2','4']
print(test)
v = test.sort()
print(test)
结果:
    ['a', 'b', 'f', 'c', 'e', 'd', 'g', '1', '7', '3', '6', '2', '4']
    ['1', '2', '3', '4', '6', '7', 'a', 'b', 'c', 'd', 'e', 'f', 'g']


 

——————————tuple(元组)————————
count 计数

test = ('a','b','f','a','a','d','g','1','7','a','6','2','4')
print(test)
v = test.count('a')
print(v)
结果:
    ('a', 'b', 'f', 'a', 'a', 'd', 'g', '1', '7', 'a', '6', '2', '4')    
    4

index 查找对应的下标号

test = ('a','b','f','a','a','d','g','1','7','a','6','2','4')
print(test)
v = test.index('a',2,10) #2是起始位置,10是终点位置 进行查找
print(v)
结果:
    3

 

——————————dict(字典)————————
fromkeys #根据序列创建key,并制定统一的值。

dic = {
    "k1": 1,
    "k2": 2,
    "k3": 3,
    "k4": 4,
}
#根据序列创建key,并制定统一的值。
v = dict.fromkeys(["k10",123,"9999"],123)
print(v)
结果:
    {'9999': 123, 123: 123, 'k10': 123}

get #根据key获取值,key不存在时,可以指定默认值(None),也只可以指定返回值

dic = {
    "k1": 1,
    "k2": 2,
    "k3": 3,
    "k4": 4,
}
v = dic.get('ke1',1111) #指定返回值 1111
print(v)
v = dic.get('ke1') #默认返回值
print(v)
结果:
    1111
    None

items  #取出所有的key,values

dic = {
    "k1": 1,
    "k2": 2,
    "k3": 3,
    "k4": 4,
}
for i,o in dic.items():
    print(i,o)
结果:
    k2 2
    k4 4
    k1 1
    k3 3

keys 取出所有的key

dic = {
    "k1": 1,
    "k2": 1,
    "k3": 1,
    "k4": 1,
}
for i in dic.keys(): #取出所有的key
    print(i)
结果:
    k1
    k2
    k4
    k3

pop #删除并获取值

dic = {
    "k1": 1,
    "k2": 2,
    "k3": 3,
    "k4": 4,
}
#删除并获取值
v = dic.pop('k1',90)  #当key不存在时,返回第二值90
print(dic,v)      #打印删除values
结果:
    {'k4': 4, 'k3': 3, 'k2': 2} 1

popitem #随机删除一个值

dic = {
    "k1": 1,
    "k2": 2,
    "k3": 3,
    "k4": 4,
}
a,c = dic.popitem() #随机删除一个值
print(dic,a,c) #打印随机删除key,values.
结果:
    {'k3': 3, 'k2': 2} k4 4

setdefault #设置key,values,如果值存在,获取当前values, 如果值不存在,添加设置的值,key:values,

dic = {
    "k1": 1,
    "k2": 2,
    "k3": 3,
    "k4": 4,
}
v = dic.setdefault('k1','123') #如果值存在,获取当前values,
print(dic,v)
v = dic.setdefault('k1121','123') # 如果值不存在,添加设置的值,key:values,
print(dic,v)
结果:
    {'k1': 1, 'k2': 2, 'k4': 4, 'k3': 3} 1
    {'k1121': '123', 'k1': 1, 'k2': 2, 'k4': 4, 'k3': 3} 123

update  #更新 update

dic = {
    "k1": 1,
    "k2": 2,
    "k3": 3,
    "k4": 4,
}
#更新 update
dic.update({'k1':123,'k123':'dasdasd'})
print(dic)
#另一种写法
dic.update(k1=3123,k5="qweqweasd")
print(dic)
结果:
    {'k123': 'dasdasd', 'k1': 123, 'k4': 4, 'k2': 2, 'k3': 3}
    {'k123': 'dasdasd', 'k1': 3123, 'k4': 4, 'k2': 2, 'k3': 3, 'k5': 'qweqweasd'}
    有值就替换掉,没有key,values,就添加上

 values # 取出所有的values

dic = {
    "k1": 1,
    "k2": 2,
    "k3": 3,
    "k4": 4,
}
for i in dic.values(): # 取出所有的values
    print(i)
结果:
    4
    1
    2
    3

 

 ps:本人新手整理资料,请各位大神勿喷,多多耐心指点!!!谢谢。

posted on 2018-04-25 22:41  I我非柠檬为何心酸I  阅读(473)  评论(1编辑  收藏  举报