>>> 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. LOJ2586 APIO2018 选圆圈

    考前挣扎 KD树好题! 暴力模拟 通过kd树的结构把子树内的圈圈框起来 然后排个序根据圆心距 <= R1+R2来判断是否有交点 然后随便转个角度就可以保持优越的nlgn啦 卡精度差评 必须写ep ...

  2. sublime text 3 快捷操作

    快捷键:1.通用 ↑↓← → 上下左右移动光标 Alt 调出菜单 Ctrl + Shift + P 调出命令板(Command Palette) Ctrl + ` 调出控制台 2.编辑 Ctrl + ...

  3. 项目中有 xxxx 不能被json序列化

    遇到这类问题 ,首先断点调试,看看要序列化的值 是一个什么类型的值 查看值得数据类型 在将值转化成可以被json序列化的对象 此时即可解决问题 如遇到  requests.post() 朝一个url发 ...

  4. 写出高性能SQL语句的十三条法则

    1. 首先要搞明白什么叫执行计划? 执行计划是数据库根据SQL语句和相关表的统计信息作出的一个查询方案,这个方案是由查询优化器自动分析产生的,比如一条SQL语句如果用来从一个10万条记录的表中查1条记 ...

  5. php strnatcmp()函数 语法

    php strnatcmp()函数 语法 作用:自然顺序法比较字符串直线往复电机 语法:strnatcmp(string1,string2) 参数: 参数 描述 string1 必须,规定要比较的第一 ...

  6. 【Java】定义Logger为什么要用static和final?

    private static final Logger logger= LoggerFactory.getLogger(ShiroConfig.class); (1)出于资源利用的考虑,Logger的 ...

  7. 10.15 sed 命令实践

    在n行前插入 [root@wen data]# sed '2i 106,dandan,CSO' person.txt101,oldboy,CEO106,dandan,CSO102,zhangyao,C ...

  8. 5 August

    P1016 旅行家的预算 单调队列. 再看看单调队列怎么用的. #include <cstdio> int n, l, r; double D, dd, d[9], C, p[9], an ...

  9. leetcode-解题记录 1108. IP 地址无效化

    题目: 给你一个有效的 IPv4 地址 address,返回这个 IP 地址的无效化版本. 所谓无效化 IP 地址,其实就是用 "[.]" 代替了每个 ".". ...

  10. MongoDB 3.6 开启慢查询

    参考:Profiling Levels:支持一下级别.0 默认的profiler level,profiler 关闭并且不收集数据.1 profiler 收集超过slowms的操作数据.2 profi ...