Python 序列类型拆包 %s 和'{}'.format 的功能差异之一
>>> 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 的功能差异之一的更多相关文章
- Python序列类型
Python序列类型 序列:字符.列表.元组 所有序列都支持迭代 序列表示索引为非负整数的有序对象集合 字符和元组属于不可变序列,列表可变 1)字符 字符串字面量:把文本放入单引号.双引号或三引号中: ...
- python 序列类型
1.不可变的序列类型:tuple.range.str.set 001:对于tuple 类型有如下几种构造方式 1.() 构造一个空的元组. 2.a | (a,) 构造一个只有一个元素的元组. 3.tu ...
- Python序列类型各自方法
在Python输入dir(str).dir(list).dir(tuple)可查看各种序列类型的所有方法. 对于某个方法不懂怎么使用的情况,可以直接help(str.split)对某个方法进行查询. ...
- Python序列类型方法
列表的常用方法 append.insert.extend.pop.remove 元组的两个方法count.index 字符串的常用方法及转义count.find.index.replace.split ...
- Python 序列类型小结
序列是python中最基本的数据结构. 序列中每一个元素都有其对应的索引,索引是从0开始,0,1,2......依次类推 python中的序列类型有:字符串str.列表list.元组tuple.Uni ...
- python序列类型及一些操作
序列分类 1.按存放的数据类型分类: 容器类型(能存放不同类型的数据):list.tuple.coolections.deque 扁平序列(只能存放一种类型的数据):str.bytes.bytearr ...
- python序列类型字符串的方法L.index()与L.find()区别
首先官方解释 S.index(sub[, start[, end]]) -> int Like S.find() but raise ValueError when the substring ...
- python高级(二)—— python内置序列类型
本文主要内容 序列类型分类: (1)容器序列.扁平序列 (2)可变序列.不可变序列 列表推导式 生成器表达式 元组拆包 切片 排序(list.sort方法和sorted函数) bisect pytho ...
- Python 基本数据类型和序列类型
python 3.6.4 中,有9种数据类型: int, float, bool, complex, list, tuple, string, set, dict (1).int 整型,不可变 (2) ...
随机推荐
- cookie记住账户密码
//cookie保存路径 String autologin=request.getParameter("autologin"); String path = request.get ...
- 运行rabbitmq
docker run -d -p 5672:5672 -p 15672:15672 --name myrabbitmq c4663bdca2cd
- C#-弄懂泛型和协变、逆变
脑图概览 泛型声明和使用 协变和逆变 <C#权威指南>上在委托篇中这样定义: 协变:委托方法的返回值类型直接或者间接地继承自委托前面的返回值类型; 逆变:委托签名中的参数类型继承自委托方法 ...
- 英语单词collaboration
collaboration 来源——github网站 https://guides.github.com/activities/hello-world/ GitHub is the best way ...
- Linux进程管理——查看内存的工具
Linux进程管理——查看内存的工具 一查看内存的工具vmstat vmstat命令:虚拟内存信息vmstat [options] [delay [count]]vmstat 2 5 [root@ce ...
- 104、Tensorflow 的变量重用
import tensorflow as tf # 在不同的变量域中调用conv_relu,并且声明我们想创建新的变量 def my_image_filter(input_images): with ...
- mysql null 值查询问题
我在开发公司内部的一个项目时遇到一个问题:select student_quality_id from STUDENT_QUALITY where mark_status=0 and batch_st ...
- git设置忽略文件.gitignore
在仓库目录下新建一个名为.gitignore的文件(因为是点开头,没有文件名,没办法直接在windows目录下直接创建,必须通过右键Git Bash,按照linux的方式来新建.gitignore文件 ...
- 抓包工具charles下载安装(MAC版)
什么是charles? charles是一个HTTP代理服务器,HTTP监视器,反转代理服务器,当浏览器连接Charles的代理访问互联网时,Charles可以监控浏览器发送和接收的所有数据.它允许一 ...
- Linux查看大文件日志
Linux 查看大日志文件1.使用 less 命令 less filename 但是使用上述命令的坏处是,默认打开的位置在第一行,并且当切换到实时滚动模式(按 F ,实现效果类似 tail -f 效果 ...