在学习笔记Task01的数据类型中已经对字符串的一些使用方法进行了介绍,现在在Task01的基础上进行一些补充。
1、字符串
var1 = 'Hello World!'
var2 = "Python Runoob"
print( "var1[0]: ", var1[0])#var1[0]: H
print ("var2[1:5]: ", var2[1:5])#var2[1:5]: ytho
a = "Hello"
b = "Python"
print( "a + b)#HelloPython
print ("a * 2 )#HelloHello
print ("a[1] ) #e
print ("a[1:4]) #ell
if( "H" in a) :
print( "H 在变量 a 中" )
else :
print( "H 不在变量 a 中" )
#H 在变量 a 中
if( "M" not in a) :
print( "M 不在变量 a 中" )
else :
print( "M 在变量 a 中")
#M 不在变量 a 中
print (r'\n')#\n
print( R'\n')#\n
print( "My name is %s and weight is %d kg!" % ('Zara', 21))
#My name is Zara and weight is 21 kg!
2、序列常用的内置函数
>>> len('st')
2
>>> len('s t')
3
>>> len("df ")
3
print(max(1, 2, 3, 4, 5)) # 5
print(max([-8, 99, 3, 7, 83])) # 99
print(max('IloveLsgoGroup')) # v
print(min(1, 2, 3, 4, 5)) # 1
print(min([-8, 99, 3, 7, 83])) # -8
print(min('IloveLsgoGroup')) # G
>>> sum(range(1,11))
55
>>> sum(range(1,11),1) #等价于1+sum(range(1,10)
56
>>> aList = [1, 2, 3]
>>> bList = [4, 5, 6]
>>> cList = zip(aList, bList)
>>> list(cList)[(1, 4), (2, 5), (3, 6)]
#注意:当两个列表的元素个数不相等时,舍弃元素个数多的那个列表的多余部分>>> x=[1,2]
>>> y=[3,4,5]
>>> k=zip(x,y)
>>> list(k)
[(1, 3), (2, 4)]
>>> t=[2,4,5]
>>> for i in enumerate(t):
print(i)
(0, 2)
(1, 4)
(2, 5)
参考文献