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.pswp.cn/news/378615.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/378615.shtml
英文地址,請注明出處:http://en.pswp.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隱藏&#…