print("-----------python-字典---------")
capitals ={'Iowa':'DesMoines','Wisconsin':'Madison'}print(capitals)print("capitals['Iowa']:", capitals['Iowa'])
capitals['Utah']='SaltLakeCity'print("添加新的键值对:", capitals)print("字典长度:",len(capitals))# 遍历字典, 遍历的是“键”print("遍历字典:")for k in capitals:print(capitals[k]," is the capital of ", k)print("打印keys:", capitals.keys())# “值”存入列表
l_capitals =list(capitals.keys())print("“值”存入列表:", l_capitals)print("打印值values:", capitals.values())print("打印items:", capitals.items())print("get(k),return 值:", capitals.get("Iowa"))
print("-----------python-字典---------")
capitals ={'Iowa':'DesMoines','Wisconsin':'Madison'}print(capitals)print("capitals['Iowa']:", capitals['Iowa'])
capitals['Utah']='SaltLakeCity'print("添加新的键值对:", capitals)print("字典长度:",len(capitals))# 遍历字典, 遍历的是“键”print("遍历字典:")for k in capitals:print(capitals[k]," is the capital of ", k)print("打印keys:", capitals.keys())# “值”存入列表
l_capitals =list(capitals.keys())print("“值”存入列表:", l_capitals)print("打印值values:", capitals.values())print("打印items:", capitals.items())print("get(k),return 值:", capitals.get("Iowa"))# 格式化字符串print("-----------python-格式化字符串---------")print("Hello","World")print("通过sep来更改分隔符:","Hello","World", sep='***')print("通过end来更改结尾字符(默认都是以换行符结尾):","Hello","World", end='***')print('\n')
name ="ct"
age =12print("%s is %d years old."%(name, age))# %f, %e, %c...# 除了格式字符之外,您还可以在%和格式字符之间包含一个格式修饰符。# 格式修饰符可以用来对指定字段宽度的值进行左对齐或右对齐。修饰符也可以用来指定字段宽度,以及小数点后的数字。
price =24
item ="banana"print("The %s costs %d cents"%(item, price))# “24.00”字符串长度为5,所以%5.2f看不出变化,%6.2f会出现一个空格print("The %+10s costs %5.2f cents"%(item, price))print("The %+10s costs %6.2f cents"%(item, price))print("The %+10s costs %10.2f cents"%(item, price))
itemdict ={"item":"banana","cost":24}print("The %(item)s costs %(cost)7.1f cents"% itemdict)# 除了使用格式字符和格式修饰符的格式字符串之外,Python字符串还包括一种format方法,# 它可以与新的Formatter类一起使用来实现复杂的字符串格式化