Python 序列类型支持拆包操作

  序列类型:字符串,列表,元祖

1、多个元素我们使用 逗号 分开,打印出来也是元祖,没有必要使用括号()

a = "电脑", "手机"
print("值为:{} ,类型为:{}".format(a, type(a)))

# 结果:值为:('电脑', '手机') ,类型为:<class 'tuple'>

2、拆包操作:所有的序列类型都支持拆包:字符串,列表,元祖

a = "电脑", "手机"
b1, b2 = a
print("c1=", b1)
print("c2=", b2)
# 结果:
# c1= 电脑
# c2= 手机

字符串拆包操作

b = "12345"
str1, str2, str3, str4, str5 = b

print(str1, str2, str3, str4, str5)

# 结果:1 2 3 4 5

*******请大家尊重原创,如要转载,请注明出处:转载自:https://www.cnblogs.com/shouhu/,谢谢!!******* 

原文地址:https://www.cnblogs.com/shouhu/p/12739822.html