Python中列表、元组、占位符的用法

来源:互联网 发布:龙虎榜数据查询 编辑:程序博客网 时间:2024/06/05 16:07

占位符

>>> a='yang'>>> b='shixian'>>> welcome='Hello,%s %s'>>> print(welcome % (a,b))Hello,yang shixian>>> spaces=' '*25>>> print(spaces)>>> print('%s subject' % spaces)                          subject>>> 

列表

列表中添加元素用append(value)、删除用remove(value)或者 del listaa[1]

>>> list3 = [1,2,3,'ab']>>> print(list3)[1, 2, 3, 'ab']>>> list3.remove('ab')>>> print(list3)[1, 2, 3]>>> list1.remove(p4)

元组

# 这里是键值关系对>>> list2={'1':'a','2':'b','3':'c','4':'d','5':'e'}# 显示键1对应的值>>> print(list2['1'])a>>> print(list2){'3': 'c', '1': 'a', '5': 'e', '2': 'b', '4': 'd'}>>> print(list2){'3': 'c', '1': 'a', '5': 'e', '2': 'b', '4': 'd'}# 删除键值>>> del list2['5']>>> print(list2){'3': 'c', '1': 'a', '2': 'b', '4': 'd'}

字符串中,如何显示变量

>>> age=25>>> if age>20:    print('You are %s years old !' % age)You are 25 years old !>>>
0 0