>>> 1, 2, 3 #这样写成一行相当于一个元组
(1, 2, 3)
>>> x = 1, 2, 3
>>> x
(1, 2, 3)
>>> type(x)
<class 'tuple'>
>>> x, y, z = 4, 6, 5
>>> x, y, z
(4, 6, 5)
>>> tx = x, y, z
>>> tx
(4, 6, 5)
>>> a, b, c = tx
>>> a
4
>>> b
6
>>> c
5
>>> (1, 3, 5)
(1, 3, 5)
>>>

>>> li = [('张三', 50), ('李四', 30)]
>>> for name,_ in li:
... print(name)
... print(_)
...
张三
50
李四
30

>>> for inf in li:
... print('%s__%s' % inf)  #这个功能只有%s 能做到, '{}'.format()做就会出错
...
张三__50
李四__30
>>> for inf in li:
... print('{}, {}'.format(inf))
...
Traceback (most recent call last):
File "<stdin>", line 2, in <module>
IndexError: tuple index out of range
>>> for inf in li:
... print('{}, {}'.format inf)
File "<stdin>", line 2
print('{}, {}'.format inf)
^
SyntaxError: invalid syntax

>>> for inf in li:
... print('{}, {}'.format(*inf)) # 如果用*拆包, 就可以了
...
张三, 50
李四, 30

'%s'无法拆字符串的包,  但是'{}'.format()却可以:

>>> st = 'abcd'
>>> *st
File "<stdin>", line 1
SyntaxError: can't use starred expression here
>>> s, *a = st
>>> a
['b', 'c', 'd']
>>> s
'a'
>>> print('%s %s %s %s' % st)

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: not enough arguments for format string

>>> print('%s %s %s %s' % (st))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: not enough arguments for format string

>>> print('%s %s %s %s' %(*st))
File "<stdin>", line 1
SyntaxError: can't use starred expression here

>>> print('{}'.format(*st))
a
>>> print('{2}'.format(*st))
c
>>>

>>> l = (4, 5)
>>> def jia(x, y):
... return x+y
...
>>> jia(*l)
9
>>>

Python 序列类型拆包 %s 和'{}'.format 的功能差异之一的更多相关文章

  1. Python序列类型

    Python序列类型 序列:字符.列表.元组 所有序列都支持迭代 序列表示索引为非负整数的有序对象集合 字符和元组属于不可变序列,列表可变 1)字符 字符串字面量:把文本放入单引号.双引号或三引号中: ...

  2. python 序列类型

    1.不可变的序列类型:tuple.range.str.set 001:对于tuple 类型有如下几种构造方式 1.() 构造一个空的元组. 2.a | (a,) 构造一个只有一个元素的元组. 3.tu ...

  3. Python序列类型各自方法

    在Python输入dir(str).dir(list).dir(tuple)可查看各种序列类型的所有方法. 对于某个方法不懂怎么使用的情况,可以直接help(str.split)对某个方法进行查询. ...

  4. Python序列类型方法

    列表的常用方法 append.insert.extend.pop.remove 元组的两个方法count.index 字符串的常用方法及转义count.find.index.replace.split ...

  5. Python 序列类型小结

    序列是python中最基本的数据结构. 序列中每一个元素都有其对应的索引,索引是从0开始,0,1,2......依次类推 python中的序列类型有:字符串str.列表list.元组tuple.Uni ...

  6. python序列类型及一些操作

    序列分类 1.按存放的数据类型分类: 容器类型(能存放不同类型的数据):list.tuple.coolections.deque 扁平序列(只能存放一种类型的数据):str.bytes.bytearr ...

  7. python序列类型字符串的方法L.index()与L.find()区别

    首先官方解释 S.index(sub[, start[, end]]) -> int Like S.find() but raise ValueError when the substring ...

  8. python高级(二)—— python内置序列类型

    本文主要内容 序列类型分类: (1)容器序列.扁平序列 (2)可变序列.不可变序列 列表推导式 生成器表达式 元组拆包 切片 排序(list.sort方法和sorted函数) bisect pytho ...

  9. Python 基本数据类型和序列类型

    python 3.6.4 中,有9种数据类型: int, float, bool, complex, list, tuple, string, set, dict (1).int 整型,不可变 (2) ...

随机推荐

  1. hive之压缩

    对数据进行压缩可以节约磁盘空间,提高系统吞吐量和性能,但是压缩和解压缩会增加CPU的开销. 1.hive的压缩编/解码器 BZip2和GZip压缩率高,但是需要消耗较多的CPU开销.LZO和Snapp ...

  2. 理解Java构造器中的"this"

    Calling Another Constructor if the first statement of a constructor has the form this(...), then the ...

  3. oracle中日期转换

    oracle中,日期转换函数有很多,常用命令如下: to_char()命令将时间戳转换为用户规定的日期格式,如: SELECT TO_CHAR(sysdate,'YYYY-MM-DD hh24:mi: ...

  4. ckeditor实现WORD粘贴图片自动上传

    自动导入Word图片,或者粘贴Word内容时自动上传所有的图片,并且最终保留Word样式,这应该是Web编辑器里面最基本的一个需求功能了.一般情况下我们将Word内容粘贴到Web编辑器(富文本编辑器) ...

  5. 原理图和PCB元件对应查找--Altium Designer

    画PCB的时候,需要经常的去查看原理图上对应的元件,元件数目少还好找,数目多了找起来就比较扯淡.还好Altium Designer提供了不错的交叉查找功能. 建议使用两个显示器,一个显示器放原理图,另 ...

  6. <三剑客> 老大:awk命令用法

    awk是一种编程语言,用于在linux/unix下对文本和数据进行处理.数据可以来自标准输入(stdin).一 个或多个文件,或其它命令的输出.它支持用户自定义函数和动态正则表达式等先进功能,是lin ...

  7. [CSP-S模拟测试]:u(差分)

    题目背景 $\frac{1}{4}$遇到了一道水题,完全不会做,于是去请教小$D$.小$D$看了一眼就切掉了这题,嘲讽了$\frac{1}{4}$一番就离开了.于是,$\frac{1}{4}$只好来问 ...

  8. jQuery:unbind方法的使用详解

    一.概述: unbind方法只能解绑用jQuery的bind方法以及用jQuery方法注册的事件处理程序.比如:$(‘a’).click(function(){})可以通过unbind解绑.用原生ad ...

  9. js判断客户端是pc还是移动端

    navigator.userAgentNavigator 对象包含有关浏览器的信息.没有应用于 navigator 对象的公开标准,不过所有浏览器都支持该对象.userAgent 属性是一个只读的字符 ...

  10. CSS 6种完全居中最佳实践(整理)

    2016年4月28日 1.最佳法: .Absolute-Center { width: 50%; height: 50%; overflow: auto; margin: auto; position ...