a =['1','2','3']print('+'.join(a))#the result : 1+2+3
1.3 split方法
作用:这是一个非常重要的字符串,它是join的逆方法,用来将字符串分割成序列
print('1+2+3+4'.split('+')) #the result : ['1','2','3','4']
1.4 strip
作用:strip 方法返回去除首位空格(不包括内部)的字符串
print(" test test ".strip())#the result :“test test”
1.5 replace
作用:replace方法返回某字符串所有匹配项均被替换之后得到字符串
print("This is a test".replace('is','is_test'))#the result : This_test is_test a test
1.6 首字母大写
>>> s ='aBdkndfkFFD'>>> s.capitalize()'Abdkndfkffd'
1.7 Pinyin 模块,将汉字转换成拼音
#! /usr/bin/env python# -*- coding: utf-8 -*-from xpinyin import Pinyin
whileTrue:
p = Pinyin()
fullname =raw_input('name:').strip()
fullname = fullname.decode('utf8')print fullname
xin = fullname[0]
ming = fullname[1:]
name = ming +'.'+ xin
username = p.get_pinyin(name,'')print username
print username +'@yiducloud.cn'
02.字符串格式化
2.1 使用百分号(%)字符串格式化
num =100print("%d to hex is %x"%(num, num))#100 to hex is 64print("%d to hex is %#x"%(num, num))#100 to hex is 0x64
2.2 使用format字符串格式化
#1. 位置参数print("{0} is {1} years old".format("tom",28))#tom is 28 years oldprint("{} is {} years old".format("tom",28))#tom is 28 years oldprint("Hi, {0}! {0} is {1} years old".format("tom",28))#Hi, tom! tom is 28 years old#2. 关键字参数print("{name} is {age} years old".format(name ="tom", age =28))#tom is 28 years old#3. 下标参数
li =["tom",28]print("{0[0]} is {0[1]} years old".format(li))#tom is 28 years old
列表
01.列表常用方法
1.1 append
#1. append用于在列表末尾追加新的对象
a =[1,2,3]
a.append(4)#the result : [1, 2, 3, 4]
1.2 count
#2. count方法统计某个元素在列表中出现的次数
a =['aa','bb','cc','aa','aa']print(a.count('aa'))#the result : 3
1.3 extend
#3. extend方法可以在列表的末尾一次性追加另一个序列中的多个值
a =[1,2,3]
b =[4,5,6]
a.extend(b)#the result :[1, 2, 3, 4, 5, 6]
1.4 index
#4. index函数用于从列表中找出某个值第一个匹配项的索引位置
a =[1,2,3,1]print(a.index(1))#the result : 0
1.5 insert
#5. insert方法用于将对象插入到列表中
a =[1,2,3]
a.insert(0,'aa')#the result : ['aa', 1, 2, 3]
1.6 pop
#6. pop方法会移除列表中的一个元素(默认是最后一个),并且返回该元素的值
a =[1,2,3]
a.pop()#the result : [1, 2]
a.pop(0)
1.7 remove
#7. remove方法用于移除列表中某个值的第一个匹配项
a =['aa','bb','cc','aa']
a.remove('aa')#the result : ['bb', 'cc', 'aa']
1.8 reverse
#8. reverse方法将列表中的元素反向存放
a =['a','b','c']
a.reverse()#the result : ['c', 'b', 'a']
1.9 sort
#9. sort方法用于在原位置对列表进行排序,意味着改变原来的列表,让其中的元素按一定顺序排列
a =['a','b','c',1,2,3]
a.sort()#the result :[1, 2, 3, 'a', 'b', 'c']
1.10 enumerate
li =[11,22,33,44,55,66]for k,v inenumerate(li,1):# 1.代表 k 从哪个数字开始print(k,v)'''
1 11
2 22
3 33
'''
#1、range根据start与stop指定的范围以及step设定的步长,生成一个序列:range([start,] stop[, step])#2、xrange 用法与 range 完全相同,所不同的是生成的不是一个list对象,而是一个生成器for i inrange(1,10,2):print(i)
1.12 列表去空
# 法1:filter(None, your_list)# 法2:while''in your_list:
your_list.remove('')# 法3:
your_list =[x for x in your_list if x !='']
02.元祖
元组定义:元组和列表一样,也是一种序列,唯一的不同是元组不能修改。
2.1 创建元组
#1. 创建元组
a =(1,2,3,4,5,6)#2. 将列表转换成元组tuple([1,2,3,4])#the result : (1, 2, 3, 4)
2.2 列表和元组常用函数
方法
作用
com(x,y)
比较两个值
len(seq)
返回序列的长度
list(seq)
把序列转换成列表
max(args)
返回序列或者参数集合中得最大值
min(args)
返回序列或者参数集合中的最小值
reversed(seq)
对序列进行反向迭代
sorted(seq)
返回已经排列的包含seq 所有元素的列表
字典
01.字典常用方法
1.1 clear
作用:clear方法清除字典中所有的项,这是一个原地操作,所以无返回值(或者说返回None)
d ={}
d['Tom']=8777# 在字典中添加数据
d['Jack']=9999print(d)#the result : {'Jack': 9999, 'Tom': 8777}
d.clear()print(d)#the result : {}
1.2 copy
作用:copy方法返回一个具有相同 ”键-值” 对的新字典,而不是副本
d ={'Tom':8777,'Fly':6666}
a = d.copy()
a['Tom']='改变后的值'print(d)#{'Fly': 6666, 'Tom': 8777}print(a)#{'Fly': 6666, 'Tom': '改变后的值'}
1.3 fromkeys
作用:fromkeys方法使用给定的键建立新的字典,每个键都对应一个默认的值None。
首先建造一个空字典,然后调用它的fromkeys方法,建立另一个字典
print({}.fromkeys(['name','age']))#the result : {'age': None, 'name': None}