一、python字符串的处理方法
>>> str = ' linzhong LongXIA '
>>> str.upper() #字符串str全部大写 ' LINZHONG LONGXIA
>>> str.lower() #字符串str全部小写 ' linzhong longxia '
>>> str.swapcase() #字符串str大小写反转 ' LINZHONG lONGxia '
>>> str.title() #字符串str首字母大写 ' Linzhong Longxia '
>>> str.strip() #默认去除str前后两端的换行符、制表符、空格 'linzhong LongXIA'
>>> str.lstrip() #去掉字符串str左边的空格 'linzhong LongXIA '
>>> str.rstrip() #去掉字符串str右边的空格 ' linzhong LongXIA'
>>> str.split() #按指定元素进行分割(默认是空格),将字符串str分割后转换成列表
['linzhong', 'LongXIA']
>>> str = 'linzhonglongxia'
>>> str.capitalize() #首字母大写,其余小写
'Linzhonglongxia'
>>> str.startswith(' lin') #判断字符串以什么开始 True
>>> str.endswith('end') #判断字符串以什么结尾 False
>>> str = 'linzhong' >>> str.isalpha() #判断str字符串是否全字母 True
>>> str = '37211986' >>> str.isdigit() #判断str字符串是否为全数字 True
>>> str = 'linzhong' >>> str.islower() #判断字符串是否全小写 True
>>> str.isupper() #判断字符串是否全大写 False
>>> str.isalnum() #判断字符串str是否全字母或是数字 True
>>> str = 'linzhongfengniao' >>> str.replace('fengniao', 'longxia') #字符串的替换,将fengniao替换为longxia 'linzhonglongxia'
>>> name = 'linzhonglongxia' >>> print('my name is {}'.format(name)) #字符串的格式化输出 my name is linzhonglongxia
二、python列表的处理方法
2.1 列表数据增加
>>> li = ['nginx', 'php', 2018, 'linux']
>>> li.insert(1, 'memcache') #按照索引的位置去插入数据 >>> print(li) ['nginx', 'memcache', 'php', 2018, 'linux']
>>> li.append('apache') #在列表的最后追加数据 >>> print(li) ['nginx', 'memcache', 'php', 2018, 'linux', 'apache']
>>> li.extend(['redhat, centos, ubuntu']) #迭代的去增加数据 >>> li.extend('openstack') >>> print(li) ['nginx', 'memcache', 'php', 2018, 'linux', 'apache', 'redhat, centos, ubuntu','o', 'p', 'e', 'n', 's', 't', 'a', 'c', 'k']
2.2 列表数据删除
>>> li.pop(1) #按照索引位置删除,有返回值 'memcache' >>> print(li) ['nginx', 'php', 2018, 'linux', 'apache', 'redhat, centos, ubuntu', 'o', 'p', ' ', 'n', 's', 't', 'a', 'c', 'k']
>>> del li[1:3] #按照切片步长删除 >>> print(li) ['nginx', 'linux', 'apache', 'redhat, centos, ubuntu', 'o', 'p', 'e', 'n', 's', 't', 'a', 'c', 'k']
>>> li.remove('apache') #按照元素去删除 >>> print(li) ['nginx', 'linux', 'redhat, centos, ubuntu', 'o', 'p', 'e', 'n', 's', 't', 'a', 'c', 'k']
>>> li.clear() #清空列表 >>> print(li) []
2.3 列表数据修改
>>> li = ['nginx', 'php', 2018, 'linux'] >>> li[2] = 'mysql' >>> print(li) ['nginx', 'php', 'mysql', 'linux']
>>> li[1:3] = ['mengconnie', '無理取閙'] >>> print(li) ['nginx', 'mengconnie', '無理取閙', 'linux']
2.4 列表数据查询
>>> print(li[1]) #按照切片去索引 mengconnie
>>> for i in li: #使用for循环进行遍历查询 ... print(i) ... nginx mengconnie 無理取閙 linux
三、python字典的处理方法
3.1 字典数据增加
>>> dic = {'name': 'lobster', 'age': 33 } >>> dic['job'] = 'IT' #按照键值对,有则改之,无则添加 >>> print(dic) {'name': 'lobster', 'age': 33, 'job': 'IT'}
>>> dic.setdefault('hobby', 'novel') #setdefault无则添加,有则不变 'novel' >>> print(dic) {'name': 'lobster', 'age': 33, 'job': 'IT', 'hobby': 'novel'} >>> dic.setdefault('name', 'dabai') 'lobster' >>> print(dic) {'name': 'lobster', 'age': 33, 'job': 'IT', 'hobby': 'novel'}
3.2 字典数据删除
3.3 字典数据修改
3.4 字典数据查询