Python字符串| 带示例的format()方法

String.format()方法 (String.format() Method)

format() method is used to format the string (in other words - we can say to achieve the functionality like printf() in C language).

format()方法用于格式化字符串(换句话说,我们可以说实现了C语言中类似于printf()的功能)。

When we need to display the values of the variables inside the string, we can format it by placing {} where we want to place the value. {} is a replacement field, that replaces with the given value in format() method.

当需要在字符串中显示变量的值时,可以通过将{}放在要放置值的位置来设置其格式。 {}是一个替换字段,它用format()方法中的给定值替换。

The field {} contains the index to replace, the value specified in the format function.

字段{}包含要替换的索引,即格式函数中指定的值。

Let suppose, there are three values specified in the format method, {0} for the first value, {1} for the second value and {2} for the third value and so on will be used.

让假设,存在用于对所述第三值的第二值和{2}等将被使用的格式方法中指定三个值,{0}为所述第一值,{1}。

Syntax:

句法:

 String.format(parameter1, parameter2,...)

Here, parameter1, parameter2, ... are the values/variables that will print within the string by replacing the field {N}. Where N is the index of the parameter.

在这里, parameter1,parameter2,...是通过替换字段{N}将在字符串中打印的值/变量。 其中N是参数的索引。

Example:

例:

    print "{0}, {1} and {2}".format("ABC", "PQR", "XYZ")
This statement will print "ABC, PQR, and XYZ"

Example program 1: (printing name, age in different output formats)

示例程序1 :(打印名称,使用不同输出格式的年龄)

# printing name
print "My name is {0}".format("Amit")
# printing name and age 
print "My name is {0}, I am {1} years old".format ("Amit", 21)
# printing name and age through variables
name = "Amit"
age = 21
print "My name is {0}, I am {1} years old".format (name,age)

Output

输出量

    My name is Amit
My name is Amit, I am 21 years old
My name is Amit, I am 21 years old

Example program 2: (Calculating SUM, AVERAGE, and printing in different output formats)

示例程序2 :(计算SUM,AVERAGE和以不同的输出格式进行打印)

a = 10
b = 20
sum = a+b
# output 1
print "sum = {0}".format (sum)
# output 2
print "Sum of {0} and {1} is = {2}".format (a, b, sum)
# finding average and printing 
a = 11
b = 20
sub = a+b
print "Average of {0} and {1} is = {2}".format (a, b, float(sum)/2)

Output

输出量

    sum = 30
Sum of 10 and 20 is = 30
Average of 11 and 20 is = 15.0

翻译自: https://www.includehelp.com/python/string-format-method-with-example.aspx

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/378615.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

PLSQL Developer使用技巧

1、PL/SQL Developer记住登陆密码在使用PL/SQL Developer时,为了工作方便希望PL/SQL Developer记住登录Oracle的用户名和密码;设置方法:PL/SQL Developer 7.1.2 ->tools->Preferences->Oracle->Logon History , “St…

3月份的总结

租房子找了个黑中介,各种扣钱,合租的违约了,押金不要了直接一走了之,水费我们承担,中介这会儿又把责任推得一干二净,还耍小聪明,非说我是两个人住的,各种费用要交两份。。。我一时气…

快速傅里叶变换(FFT)——按频率抽取DIF的基

目录【1】回顾DIT【2】算法原理【3】运算特点【1】回顾DIT https://blog.csdn.net/qq_42604176/article/details/105559756 【2】算法原理 设序列点数:N2^M,M为正整数。将输入序列按照前一半、后一半分开。(并非按照奇偶分) 由于&#xf…

02.2-元素定位(XPath)

XML路径语言用来确定XML文档中某部分位置的语言XPath用于在XML文档中通过元素和属性进行导航XPath遵守W3C标准XPath节点类型: 元素、属性、文本、命名空间、指令处理、注释、文档 通过路径表达式从XML文档中选取节点或节点设置 表达式结果说明/xxx选取根节点xxx/xx…

android ImageView 之 android:scaleTye=

原文:http://juliaailse.iteye.com/blog/1409317 1、scaleType“matrix” 是保持原图大小、从左上角的点开始,以矩阵形式绘图。 2、scaleType“fitXY” 是将原图进行横方向(即XY方向)的拉伸后绘制的。 3、scaleType“fitStart…

acquire方法_Python锁类| 带有示例的acquire()方法

acquire方法Python Lock.acquire()方法 (Python Lock.acquire() Method) acquire() is an inbuilt method of the Lock class of the threading module in Python. acquisition()是Python中线程模块的Lock类的内置方法。 This method is used to acquire a lock, either block…

VSS2008 安装silverlight3.0步骤

需要的Q我359273753 我是新手不知道在哪里上传附件 汗一个转载于:https://www.cnblogs.com/ganler1988/archive/2011/03/17/1987367.html

php字符串对象,PHP字符串到对象名称

好的我有一个字符串……$a_string "Product";我想在调用这样的对象时使用这个字符串:$this->$a_string->some_function();狄更斯如何动态调用该对象?(不要以为我在PHP 5心中)解决方法:所以你要使用的代码是:$a_string &quo…

莫比乌斯函数---C++

【问题描述】 莫比乌斯函数,数论函数,由德国数学家和天文学家莫比乌斯(Mobius,1790-1868)提出。梅滕斯(Mertens)首先使用μ(n)作为莫比乌斯函数的记号。而据说,高斯(Gauss)比莫比乌斯早三十年就曾考虑过这个函数。莫比乌斯函数在数…

Opencv——findContours函数再探(由轮廓联想连通域)

目录关于调参的一些思考分析图像的一些角度面积、周长、矩形度、圆形度、宽长比例1:找出汽车轮毂圆孔(从轮廓和连通域两个角度)例2:找出芯片中间正方形物体例3:桌面上橘色物体总结关于调参的一些思考 合理的参数设置&…

stl vector 函数_vector :: crend()函数以及C ++ STL中的示例

stl vector 函数C vector :: crend()函数 (C vector::crend() function) vector::crend() is a library function of "vector" header, it is used to get the first element of a vector from reverse ending, it returns a const reverse iterator pointing to th…

.Net DateTime.ToString 格式化输出 (转载)

原文 虽然 System.DateTime 本身已经具有了不少现成的格式化输出,例如: ToLongDateString, ToShortTimeString, ToUniversalTime 等,但是却远远不能满足我们实际的需要,这就要用到了 DateTime.ToString,就要提到 DateT…

modelsim 编译 xilinx库

1.为单个工程加入库 在某一个目录建立工程 然后 vlib unisim vcom -work unsim *.vhd 然后就加入了unisim库 如果是windows的话,工程文件mpf应该是记录了这个库的信息,所以重新打开这个工程时,依然有这个库 linux,不用gui界面…

php 字符串匹配 like,ThinkPHP like模糊查询,like多匹配查询,between查询,in查询,一般查询书写方法...

搜索热词ThinkPHP的数据库条件查询语句有字符串式,数组式书写方法字符串式即是原生式,数组式查询语句因书写方式与特定字符的原因比较复杂,下面为大家例出了常用的ThinkPHP数组式查询语句的使用方法ThinkPHP一般查询$data_gt[id]array(gt,8);…

C++---汉明距离

两个整数之间的汉明距离指的是这两个数对应二进制位不同的位置的数目。 【输入形式】 给出两个整数x和y(0<x,y<2^31)&#xff0c;用空格分隔 【输出形式】 输出他们之间的汉明距离 【样例输出】 1 4 【样例说明】 00000000 00000000 00000000 00000001 00000000 00000000…

Opencv基础画图函数——line、circle、rectangle、Rect、ellipse、polylines、putText函数的用法

目录1、line函数2、circle函数3、rectangle、Rect函数4、ellipse函数5、polylines函数6、随机初始化颜色7、putText函数总结1、line函数 line(img,(0,0),(511,511),(255,0,0),5)这个函数有5个参数&#xff0c;img是图像名称&#xff0c;起点坐标&#xff0c;终点坐标&#xff…

GCC 里面的一些命令

记录一下常用GCC 相关的命令和参数 ldd ---> print share library dependenciy LD_LIBRARY_PATH---> environment variable, it will search the path accord to this variable. Also check the ldd to verify this environmental variable ldconfig-----> configure…

理解关联容器“map”的关键点

map有一个构造函数: map<k, v> m(b, e); 《C Primer》解释为&#xff1a;“创建 map 类型的对象 m&#xff0c; 存储迭代器 b 和 e 标记的范围内所有元素的副本&#xff0c;元素的类型必须能转换为 pair<const k, v>”&#xff0c;这个构造函数理解起来没有另外两个…

c语言中图形驱动程序功能_C / C ++中的图形:一些更有趣的功能

c语言中图形驱动程序功能In this Advance Learning Tutorial of C / C today, we are going to tell you about some of the functions that can be used to make the program more attractive. This works on both text and graphics modes. That is why knowing these funct…

php 载入css就可以显示,如何在进度条加载后显示页面

1.思路&#xff1a;加入很多图片&#xff0c;以延迟加载时间&#xff0c;实现加载完后显示图片。定义一个外层p&#xff0c;覆盖住图片&#xff0c;在内层p中引入加载时显示的图片&#xff0c;让内层p居中在页面上&#xff0c;利用setInterval定时器设置3秒后将外层p隐藏&#…