Python3基础 str format 位置参数与关键字参数
- Python : 3.7.0
- OS : Ubuntu 18.04.1 LTS
- IDE : PyCharm 2018.2.4
- Conda : 4.5.11
- typesetting : Markdown
code
coder@Ubuntu:~$ source activate py37
(py37) coder@Ubuntu:~$ ipython
Python 3.7.0 (default, Jun 28 2018, 13:15:42)
Type 'copyright', 'credits' or 'license' for more information
IPython 6.5.0 -- An enhanced Interactive Python. Type '?' for help.
In [1]: # 位置参数
In [2]: "{0}{1}{2}{1}{0}".format("a","b","c") # "a"填的是{0} "b"填的是{1} "c"
...: 填的是2
Out[2]: 'abcba'
In [3]:
In [3]: # 关键字参数
In [4]: "{var1}{var2}{var3}{var2}{var1}".format(var1="a",var2="b",var3="c")
Out[4]: 'abcba'
In [5]:
In [5]: # 混合
In [6]: "{0}{var1}{var2}".format("a",var1="b",var2="c") # 可以这么做,但不推荐
Out[6]: 'abc'
In [7]:
In [7]: "{var}{0}".format("a",var="b")
Out[7]: 'ba'
In [8]: exit
(py37) coder@Ubuntu:~$ source deactivate
coder@Ubuntu:~$
more knowledge
def format(self, *args, **kwargs): # known special case of str.format
"""
S.format(*args, **kwargs) -> str
Return a formatted version of S, using substitutions from args and kwargs.
The substitutions are identified by braces ('{' and '}').
"""
pass
coder@Ubuntu:~$ source activate py37
(py37) coder@Ubuntu:~$ ipython
Python 3.7.0 (default, Jun 28 2018, 13:15:42)
Type 'copyright', 'credits' or 'license' for more information
IPython 6.5.0 -- An enhanced Interactive Python. Type '?' for help.
In [1]: # 错误的示范
In [2]: "{var}{0}".format(var="b","a")
File "<ipython-input-2-ec3e374dcf52>", line 1
"{var}{0}".format(var="b","a")
^
SyntaxError: positional argument follows keyword argument
In [3]: exit
(py37) coder@Ubuntu:~$ source deactivate
coder@Ubuntu:~$
resource
- [文档] docs.python.org/3
- [规范] www.python.org/dev/peps/pep-0008
- [规范] zh-google-styleguide.readthedocs.io/en/latest/google-python-styleguide/python_language_rules
- [源码] www.python.org/downloads/source
- [ PEP ] www.python.org/dev/peps
- [平台] www.cnblogs.com
- [平台] gitee.com
Python具有开源、跨平台、解释型、交互式等特性,值得学习。
Python的设计哲学:优雅,明确,简单。提倡用一种方法,最好是只有一种方法来做一件事。
代码的书写要遵守规范,这样有助于沟通和理解。
每种语言都有独特的思想,初学者需要转变思维、踏实践行、坚持积累。
Python3基础 str format 位置参数与关键字参数的更多相关文章
- Python3基础 str format 输出花括号{}
Python : 3.7.0 OS : Ubuntu 18.04.1 LTS IDE : PyCharm 2018.2.4 Conda ...
- Python3基础 str format 四舍六入五凑偶 保留一位小数
Python : 3.7.0 OS : Ubuntu 18.04.1 LTS IDE : PyCharm 2018.2.4 Conda ...
- 第5章函数进阶 第5.1节 Python函数的位置参数、关键字参数精讲
前面第二章简单介绍了函数定义的语法,经过后面一系列的学习,函数有必要再次介绍一下相关内容. 一. 关于函数的语法 1. 语法 def 函数名([参数]): 函数文档字符串 函数体 ...
- python函数—形参、实参、位置参数、关键字参数
1.通过def function_name([parameter]): 定义,函数一遇到return即结束运行.如果函数没有定义返回值,则返回None,如果定义了一个返回值,则返回该对象,如果一个re ...
- python 位置参数和关键字参数 *args **kwargs
#!/usr/bin/env pythondef foo(*args,**kwargs): print('args: {0}'.format(args)) print('kwargs {0}'.for ...
- python函数 位置参数,关键字参数,可变参数优先级
def fun(arg,args=1,*arg,**keywords): python 一共有这四类参数,第一类最常见,不用多说,第二类,关键字参数,python能通过关键字找到参数,python函数 ...
- Python - 3.8 新特性之仅位置参数 & 仅关键字参数
前置知识 Python 函数:https://www.cnblogs.com/poloyy/p/15092393.html 什么是仅限位置形参 仅限位置形参是 Python 3.8 才有的新特性 新增 ...
- python的位置参数、关键字参数、收集参数,关键字收集参数混合调用问题
参数混合调用顺序用法: 函数中参数顺序为:普通参数,收集参数,关键字参数,关键字收集参数,其顺序不能颠倒,颠倒会报错. 普通参数.关键字参数可以有n个,对量没有具体要求,收集参数和关键字收集参数要么没 ...
- Python3基础 str partition 以参数字符串切分字符串,只切分为三部分
Python : 3.7.0 OS : Ubuntu 18.04.1 LTS IDE : PyCharm 2018.2.4 Conda ...
随机推荐
- AllowOverride None
PHP Advanced and Object-Oriented Programming Larry Ullman <Directory /> AllowOverride None < ...
- c#导出ListView中的数据到Excel表格
1.添加组件:Microsoft.Office.Interop.Excel 步骤:右键点击“引用”--->添加引用--->COM--->Microsoft.Office.Intero ...
- Spark-自定义排序
一.自定义排序规则-封装类 import org.apache.spark.rdd.RDD import org.apache.spark.{SparkConf, SparkContext} /** ...
- sql server dba常用概念、操作分析char,varchar,nvarchar,varchar(max)
1.设计表时如何使用char 与 varchar? 请写出你对varchar(max)的理解. 1.设计表时如何使用char 与 varchar? 请写出你对varchar(max)的理解. char ...
- query:callback
function getName(callback) { setTimeout(function() { callback('Aaron') }, 1000) } //等待callback回调 get ...
- 包含.h就可以用其对应的函数
//callee.h 被调用者 #pragma once void display(); //展示函数 //callee.cpp 被调用者 #include "callee.h" ...
- qsv转换为mp4
1:下载 装换工具:http://www.downza.cn/soft/27484.html 2:双击打开exe可执行程序. 3:添加要转换的文件,和转换后要存储的位置 4:开始转换,转换为flv格 ...
- 二进制协议 vs 文本协议
二进制协议 vs 文本协议 在服务器程序开发过程中,各个服务直接需要进行交互.这样就需要定义消息的协议,一般来说协议主要包括二进制协议和文本协议,下面就我在工作中用到的两种协议说说自己的看法. 1 二 ...
- SQL Server 将查询结果导出插入(insert)语句的简单方式
转自 http://blog.csdn.net/danny_style/article/details/45166391 1.首先将查询结果添加到一个原数据库中不存在的表,表名随意命名. 例:SELE ...
- FAFU 1136 最长递增子序列
http://acm.fafu.edu.cn/problem.php?id=1136 根据dp建边,建边的时候记得判断如果原本数的大小就ok了 好久没在自家OJ上刷了 #include <ios ...