Python2.6 开始,新增了一种格式化字符串的函数 str.format(),它增强了字符串格式化的功能。

基本语法是通过 {} 和 : 来代替以前的 % 。

format 函数可以接受不限个参数,位置可以不按顺序。

代码如下图:

#coding="utf-8"
print("{} {}".format("hello", "world") )
print("{0} {1}".format("hello", "world"))
print("{1} {0} {1}".format("hello", "world"))
print("网站名:{name}, 地址 {url}".format(name="菜鸟教程", url="www.runoob.com"))
#set the value by dict
site = {"name": "菜鸟教程", "url": "www.runoob.com"}
print("网站名:{name}, 地址 {url}".format(**site)) 运行结果如下:

F:\dev\python\python.exe F:/pyCharm/practice/config_dir/zip_demo.py
hello world
hello world
world hello world
网站名:菜鸟教程, 地址 www.runoob.com
网站名:菜鸟教程, 地址 www.runoob.com

Process finished with exit code 0

python 内置函数format的更多相关文章

  1. Python 内置函数 —— format

    科学计数法: >> format(2**20, '.2e') '1.05e+06' 小数 ⇒ 百分数 >> format(.1234, '.1%') 12.3%

  2. python内置函数

    python内置函数 官方文档:点击 在这里我只列举一些常见的内置函数用法 1.abs()[求数字的绝对值] >>> abs(-13) 13 2.all() 判断所有集合元素都为真的 ...

  3. python 内置函数和函数装饰器

    python内置函数 1.数学相关 abs(x) 取x绝对值 divmode(x,y) 取x除以y的商和余数,常用做分页,返回商和余数组成一个元组 pow(x,y[,z]) 取x的y次方 ,等同于x ...

  4. Python 内置函数笔记

    其中有几个方法没怎么用过, 所以没整理到 Python内置函数 abs(a) 返回a的绝对值.该参数可以是整数或浮点数.如果参数是一个复数,则返回其大小 all(a) 如果元组.列表里面的所有元素都非 ...

  5. 【转】python 内置函数总结(大部分)

    [转]python 内置函数总结(大部分) python 内置函数大讲堂 python全栈开发,内置函数 1. 内置函数 python的内置函数截止到python版本3.6.2,现在python一共为 ...

  6. python内置函数,匿名函数

    一.匿名函数 匿名函数:为了解决那些功能很简单的需求而设计的一句话函数 def calc(n): return n**n print(calc(10)) #换成匿名函数 calc = lambda n ...

  7. python 内置函数总结(大部分)

    python 内置函数大讲堂 python全栈开发,内置函数 1. 内置函数 python的内置函数截止到python版本3.6.2,现在python一共为我们提供了68个内置函数.它们就是pytho ...

  8. Python之路(第八篇)Python内置函数、zip()、max()、min()

    一.python内置函数 abs() 求绝对值 例子 print(abs(-2)) all() 把序列中每一个元素做布尔运算,如果全部都是true,就返回true, 但是如果是空字符串.空列表也返回t ...

  9. python内置函数大全(分类)

    python内置函数大全 python内建函数 最近一直在看python的document,打算在基础方面重点看一下python的keyword.Build-in Function.Build-in ...

随机推荐

  1. python中由于中文路径引起的os.path.isfile(imgpath) == False问题

    昨天在用python脚本处理文件的时候,遇到了题述问题,明明文件时存在的,但是在用os.path.isfile(imgpath) == False进行判断的时候总是成立,在一开始以为是正反斜杠wind ...

  2. SmokePing介绍

    一.SmokePing是什么? smokeping是rrdtool的作者Tobi Oetiker的作品,用Perl语言写的,主要是监视网络性能,所以它在图形显示方面有很大优势,也是一个很有特点的ope ...

  3. Redis所支持的数据结构

    1.启动Redis2.Redis所支持的数据结构 2.1.Redis常用操作 2.2.String类型及操作 2.3.Hash类型及操作 2.4.List类型及操作 2.5.Set类型及操作 2.6. ...

  4. 关于linux下mysql安装和卸载

    卸载:https://www.cnblogs.com/Lenbrother/articles/6203620.html 卸载Mysql 找到了这篇文章:http://zhangzifan.com/ce ...

  5. block详解

    Objective-C 中 Block 有三种类型: NSStackBlock 存储于栈区 NSGlobalBlock 存储于程序数据区 NSMallocBlock 存储于堆区 block 内部没有引 ...

  6. 30个redis.conf 配置项说明

    redis.conf 配置项说明如下: 1. Redis默认不是以守护进程的方式运行,可以通过该配置项修改,使用yes启用守护进程 daemonize no 2. 当Redis以守护进程方式运行时,R ...

  7. update moudle 调用方式

    向数据库中添加数据  ztt_teacher 1:  创建一个 function moudle,设置该moudle类型为  update moudle 2: 向数据库添加数据的代码 FUNCTION ...

  8. 【LeetCode每天一题】 Merge k Sorted Lists(合并K个有序链表)

    Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. E ...

  9. [LeetCode] 130. Surrounded Regions_Medium tag: DFS/BFS

    Given a 2D board containing 'X' and 'O' (the letter O), capture all regions surrounded by 'X'. A reg ...

  10. react 写一个贪吃蛇

    示例: 全部代码如下: snake.jsx import React, { Component } from 'react'; import PropTypes from 'prop-types'; ...