# 判断是否属于序列
lst = [1,2,3,4,5,6,7,23]
a,b,c = 1,2.0,'hello'
print(a in lst)
print(b in lst)
print(c not in lst)
~~~~~~~~~~~~~~~~~~~~~~
True
True
True
# 步长
print(lst)
print(lst[2::2])
~~~~~~~~~~~~~~~~~~~~~~
[1, 2, 3, 4, 5, 6, 7, 23]
[3, 5, 7]
#列表常用操作--添加元素 append ; extend
a = [1,2,3]
b = [4,5,6]
a.append(b)
print(a)
a = [1,2,3]
a.extend(b)
print(a)
~~~~~~~~~~~~~~~~~~~~~~~
[1, 2, 3, [4, 5, 6]]
[1, 2, 3, 4, 5, 6]
#列表常用操作--删除元素 remove;del
lst = [0,1,2,3,4,5,6,7,8,9]
lst.remove(7)
print(lst)
del lst[5]
del lst[2:4]
print(lst)
lst.pop(5)
print(lst)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[0, 1, 2, 3, 4, 5, 6, 8, 9]
[0, 1, 4, 6, 8, 9]
[0, 1, 4, 6, 8]
#字符串常用操作
#转义字符 \
print('C\\Users\\hacks')
print(r'C\Users\hacks')
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
C\Users\hacks
C\Users\hacks
#序列通用功能
print('a' not in 'werferf')
print('handsome '*4)
st = 'abcdefg'
print(st[2],st[-1])
print(st[:2])
print(st[::2])
print(st.index('g'))
print('st长度为:',len(st))
~~~~~~~~~~~~~~~~~~~~~~~~~
True
handsome handsome handsome handsome
c g
ab
aceg
6
st长度为: 7
#字符串常用功能
st = "I'm handsome!"
st2 = st.replace('handsome','ugly')
print(st)
print(st2)
st = 'hahahaha'
st2 = st.replace('ha','he',2)
print(st2)
st = 'poi01,116.12345,39.98485'
lst = st.split(',')
print(lst)
m = ';'
st = m.join(lst)
print(st)
st = 'abcdefg'
print(st.startswith('a'),st.endswith('a'))
st = 'aBCDeF jka'
print(st.upper())
print(st.lower())
print(st.swapcase())
print(st.capitalize())
st = '13342324'
print(st.isnumeric())
st = 'kwejjoegr'
print(st.isalpha())
st = 'aaahh '
print(st.rstrip())
st = 'aaahhh*****'
print(st.rstrip('*'))
st = '****aaahhh****'
print(st.strip('*'))
st = '***aaa***hhh***'
print(st.strip('*'))
~~~~~~~~~~~~~~~~~~~~~~~~~~~
I'm handsome!
I'm ugly!
hehehaha
['poi01', '116.12345', '39.98485']
poi01;116.12345;39.98485
True False
ABCDEF JKA
abcdef jka
AbcdEf JKA
Abcdef jka
True
True
aaahh
aaahhh
aaahhh
aaa***hhh
#格式化字符:在字符串中插入变量
name = '波哥'
is_goodman = '%s is 好人' %name
print(is_goodman)
name1,name2 = '波哥','波妞'
#print('%s和%s是好人' % ('波哥','波妞'))
print('%s和%s是好人 '%(name1,name2))
m = 3.1415926
print('pi is %f' %m)
print('pi is %.2f' %m)
print('pi is %.3f' %m)
m = 10.6
print('m is %i' %m)
print('m is %.0f' %m)
m = 100
print('haha %+i' %m)
print('haha %i' %m)
m = -100
print('haha %+i' %m)
print('haha %i' %m)
print('{}是好人'.format('波哥'))
print("{}{}{}".format('a','b','c'),'\n'
'{0}{2}{0}{0}'.format('a','b','c'))
print('我的工作是{work}'.format(work='设计'))
~~~~~~~~~~~~~~~~~~~~~
波哥 is 好人
波哥和波妞是好人
pi is 3.141593
pi is 3.14
pi is 3.142
m is 10
m is 11
haha +100
haha 100
haha -100
haha -100
波哥是好人
abc
acaa
我的工作是设计