•        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 位置参数与关键字参数的更多相关文章

  1. Python3基础 str format 输出花括号{}

             Python : 3.7.0          OS : Ubuntu 18.04.1 LTS         IDE : PyCharm 2018.2.4       Conda ...

  2. Python3基础 str format 四舍六入五凑偶 保留一位小数

             Python : 3.7.0          OS : Ubuntu 18.04.1 LTS         IDE : PyCharm 2018.2.4       Conda ...

  3. 第5章函数进阶 第5.1节 Python函数的位置参数、关键字参数精讲

    前面第二章简单介绍了函数定义的语法,经过后面一系列的学习,函数有必要再次介绍一下相关内容. 一.    关于函数的语法 1.    语法 def 函数名([参数]): 函数文档字符串 函数体      ...

  4. python函数—形参、实参、位置参数、关键字参数

    1.通过def function_name([parameter]): 定义,函数一遇到return即结束运行.如果函数没有定义返回值,则返回None,如果定义了一个返回值,则返回该对象,如果一个re ...

  5. python 位置参数和关键字参数 *args **kwargs

    #!/usr/bin/env pythondef foo(*args,**kwargs): print('args: {0}'.format(args)) print('kwargs {0}'.for ...

  6. python函数 位置参数,关键字参数,可变参数优先级

    def fun(arg,args=1,*arg,**keywords): python 一共有这四类参数,第一类最常见,不用多说,第二类,关键字参数,python能通过关键字找到参数,python函数 ...

  7. Python - 3.8 新特性之仅位置参数 & 仅关键字参数

    前置知识 Python 函数:https://www.cnblogs.com/poloyy/p/15092393.html 什么是仅限位置形参 仅限位置形参是 Python 3.8 才有的新特性 新增 ...

  8. python的位置参数、关键字参数、收集参数,关键字收集参数混合调用问题

    参数混合调用顺序用法: 函数中参数顺序为:普通参数,收集参数,关键字参数,关键字收集参数,其顺序不能颠倒,颠倒会报错. 普通参数.关键字参数可以有n个,对量没有具体要求,收集参数和关键字收集参数要么没 ...

  9. Python3基础 str partition 以参数字符串切分字符串,只切分为三部分

             Python : 3.7.0          OS : Ubuntu 18.04.1 LTS         IDE : PyCharm 2018.2.4       Conda ...

随机推荐

  1. js-之NaN和isNaN

    NaN (not is number) 不是一个数字的意思,在js中整型和浮点数都是Number类型. 除此之外,Number还有一个特殊的值,NaN. 一.可能会产生NaN值的情况 1.表达式计算, ...

  2. getopts shell command -options parameters

    说明:原文网址http://blog.chinaunix.net/uid-26807463-id-3151601.html 获取UNIX类型的选项: unix有一个优点就是标准UNIX命令在执行时都具 ...

  3. 新建虚拟机_XP系统(一)

    准备工作:(1)安装VMware (2)下载系统镜像文件 1.新建虚拟机 2.下一步,选择“稍后安装操作系统” 3.选择操作系统类型和版本 4.设置虚拟机名称和安装位置 (安装路径空间要足够大) 5. ...

  4. 编译错误 ----- /usr/bin/ld: cannot find -lc

    yum install glibc-static glib-static是Gcc链接时使用到的库.

  5. col-md-1

    .col-md-12 {    width: 100%;  }  .col-md-11 {    width: 91.66666666666666%;  }  .col-md-10 {    widt ...

  6. 安恒X计划12月月赛

    ezweb 主要是序列化问题.没有PHP环境,在线运行的.实例化对象之后修改一下file.然后echo输出序列化的结果.不过下面有一个正则检查.数字前加一个+,影响了正则的匹配,但是对于序列化的还原没 ...

  7. SQL中常见语句

    SQL中常见语句笔记: --替换字段中的回车符和换行符 ) ), '') --删除表命令 DROP TABLE [dbo].[MGoods_Test] --删除表中数据命令 DELETE FROM [ ...

  8. checkBox的使用和事件监听

    直接上代码: <!DOCTYPE html> <html> <head> <title></title> </head> < ...

  9. 大神的博客地址liferay

    http://www.huqiwen.com/category/technology-share/liferay/

  10. YUV编码格式

    YUV是被欧洲电视系统采用的一种颜色编码方法.在现代彩色电视系统中,通常采用三管彩色摄影机或彩色CCD摄像机取像,然后把取 得的彩色图像信号经过分色,分别放大校正后得到RGB,在经过矩阵变换电路,得到 ...