Ⅰ python format鍑芥暟
python format鍑芥暟镄勭敤娉曟槸浠涔埚憿锛熶笉鐭ラ亾镄勫皬浼欎即𨱒ョ湅鐪嫔皬缂栦粖澶╃殑鍒嗕韩钖!
python format鍑芥暟鐢ㄦ硶璇﹁В锛
1銆佷綅缃
print(hello {0}, this is {1}..format(world, python)) # 镙规嵁浣岖疆涓嬫爣杩涜屽~鍏
print(hello {}, this is {}..format(world, python)) # 镙规嵁椤哄簭镊锷ㄥ~鍏
print(hello {0}, this is {1}. {1} is a new language..format(world, python)) # 钖屼竴鍙傛暟鍙浠ュ~鍏呭氭
杈揿嚭锛
hello world, this is python.
hello world, this is python.
hello world, this is python. python is a new language.
2銆乲ey
obj = world
name = python
print(hello {obj}, this is {name}..format(obj = obj, name = name))
杈揿嚭锛
hello world, this is python.
3銆佸垪琛
list = [world, python]
print(hello {names[0]}, this is {names[1]}..format(names = list))
杈揿嚭锛
hello world, this is python.
4銆佸瓧鍏
dict = {obj:world, name:python}
print(hello {names[obj]}, this is {names[name]}..format(names = dict))
杈揿嚭锛
hello world, this is python.
娉ㄦ剰锛
璁块梾瀛楀吀镄 key锛屼笉鐢ㄥ紩鍙枫
5銆佺被灞炴
class Names():
obj = world
name = python
print(hello {names.obj}, this is {names.name}..format(names = Names))
杈揿嚭锛
hello world, this is python.
6銆侀瓟娉曞弬鏁
args = [,, inx]
kwargs = {obj: world, name: python}
print(hello {obj}{} this is {name}..format(*args, **kwargs))
杈揿嚭锛
hello world, this is python.
娉ㄦ剰锛
杩欓噷镄 format(*args, **kwargs) 绛変环浜 format(,, inx, obj = world, name = python)銆
浜屻佹暟瀛楁牸寮忓寲
涓夈佸叾浠栫敤娉
1銆佽浆涔
print({{hello}} {{{0}}}.format(world))
杈揿嚭锛
{hello} {world}
2銆乫ormat 浣滀负鍑芥暟鍙橀噺
name = python
hello = hello, welcome to {} world!.format
print(hello(name))
杈揿嚭锛
hello, welcome to python world!
3銆佹牸寮忓寲 datatime
from datetime import datetime
now = datetime.now()
print({:%Y-%m-%d %X}.format(now))
杈揿嚭锛
2020-12-15 19:46:24
4銆亄}鍐呭祵{}
print(hello {0:{1}} .format(world, 10))
杈揿嚭锛
hello world
Ⅱ format鍦╬ython涓镄勭敤娉曚緥瀛
format鍦╬ython涓镄勭敤娉旷ず渚嬫湁锘烘湰鐢ㄦ硶銆佹寚瀹氢綅缃鎻掑叆鍊笺佷娇鐢ㄥ叧阌瀛楀弬鏁版彃鍏ュ笺佹牸寮忓寲鏁板瓧銆佹牸寮忓寲镞ユ湡銆
1銆佸熀链鐢ㄦ硶锛歯ame = "Alice"age = 25message = "My name is {}, and I am {} years old.".format(name, age)print(message)My name is Alice, and I am 25 years old銆
2銆佹寚瀹氢綅缃鎻掑叆鍊硷细pythonCopy codemessage = "My name is {0}, and I am {1} years old.".format("Bob", 30)print(message)csharpCopy codeMy name is Bob, and I am 30 years old銆
3銆佷娇鐢ㄥ叧阌瀛楀弬鏁版彃鍏ュ硷细pythonCopy codemessage = "My name is {name}, and I am {age} years old.".format(name="Charlie", age=35)print(message)csharpCopy codeMy name is Charlie, and I am 35 years old銆
4銆佹牸寮忓寲鏁板瓧锛歱ythonCopy codepi = 3.14159formatted_pi = "The value of pi is {:.2f}".format(pi)print(formatted_pi)csharpCopy codeThe value of pi is 3.14銆
5銆佹牸寮忓寲镞ユ湡锛歱ythonCopy codefrom datetime import datetimetoday = datetime.today()formatted_date = "Today's date is {:%Y-%m-%d}".format(today)print(formatted_date)mathematicaCopy codeToday's date is 2023-06-03銆
python璇瑷镄勪紭锷
1銆佺亩娲佹槗璇伙细Python閲囩敤绠娲佽屾竻鏅扮殑璇娉曪纴浠g爜鏄扑簬缂栧啓鍜岄槄璇汇傚畠寮鸿皟鍙璇绘э纴浣垮缑锲㈤槦鎴愬憳鑳藉熸洿杞绘涧鍦板岗浣滃拰缁存姢浠g爜銆侾ython镄勮娉旷粨鏋勬竻鏅扮亩娲侊纴鍑忓皯浜嗗啑浣欑殑璇娉曞厓绱狅纴浣垮缑寮鍙戜汉锻桦彲浠ユ洿涓撴敞浜庨梾棰樼殑瑙e喅銆
2銆佸紑鍙戞晥鐜囬珮锛歅ython鎻愪緵浜嗕赴瀵岀殑镙囧嗳搴揿拰绗涓夋柟搴掳纴娑电洊浜嗗悇绉嶅姛鑳藉拰浠诲姟銆傝繖浜涘簱鎻愪緵浜嗗ぇ閲忕殑鐜版垚妯″潡鍜屽嚱鏁帮纴浣垮紑鍙戜汉锻樿兘澶熷揩阃熸瀯寤哄簲鐢ㄧ▼搴忥纴镞犻渶浠庡ご寮濮嬬紪鍐欐墍链変唬镰併傛ゅ栵纴Python鍏锋湁锷ㄦ佺被鍨嬬郴缁熷拰镊锷ㄥ唴瀛樼$悊锛屽噺灏戜简寮鍙戜汉锻樼殑宸ヤ綔閲忋
3銆佸箍娉涘簲鐢锛歅ython琚骞挎硾搴旂敤浜庡悇涓棰嗗烟锛屽傜戝﹁$畻銆佹暟鎹鍒嗘瀽銆佷汉宸ユ櫤鑳姐佹満鍣ㄥ︿範銆乄eb寮鍙戠瓑銆傚畠鏄涓绉嶉氱敤镄勭紪绋嬭瑷锛岄傜敤浜庡悇绉崭换锷″拰椤圭洰銆侾ython𨰾ユ湁寮哄ぇ镄勫簱鍜屽伐鍏风敓镐佺郴缁燂纴鎻愪緵浜嗕赴瀵岀殑瑙e喅鏂规埚拰鏀鎸併
Ⅲ 利用Python编写程序,输入一个三位数,分离出各位数上的数字并输出
前面跟大家分享过占位符%,最近发现了个更好用的格式化输出函数format,故做了个总结,下面将是对format函数的用法全解。
首先,format是一个字符串中的函数 表达式为 str.format(),采用了 {} 来代替占位符 %
format(value, format_spec='', /)
这个为调用格式 value 表示格式化输入的值(参数),可以接受不限个参数
format函数的位置参数用法 “{0}”.format(value)
"The dog is {} years old,name {}".format(1,"DaiMo")#大括号里没有设置位置参数
结果为:'The dog is 1 years old,name DaiMo'
这是默认位置参数状态下的输出结果,第一个{}对应format函数的第一个参数,
第二个{}对应着第二个参数,以此类推,一一对应。
"The dog is {0} years old,name {1}".format(1,"DaiMo")
结果为:'The dog is 1 years old,name DaiMo'
"The dog is {1} years old,name {0}".format(1,"DaiMo")
结果为:'The dog is DaiMo years old,name 1'
通过位置的改变来让替换的值根据位置改变
format函数的字符填充用法 “{:}”.format()
说到这一点,可能稍微有点复杂,为了方便理解,在引入format函数的调用格式
'''
{ index : [ [fill] align] [sign] [+] [width] [.precision] [type] ] }
index 为位置参数,从0,1,……,开始
fill:指定空白处填充的字符,当填充字符为逗号(,)且作用于整数或浮点数时,
该整数(或浮点数)会以逗号分隔的形式输出(千分位输出),例如(1000000会输出 1,000,000)
align:指定数据的对齐方式(、^ 分别为左对齐、右对齐、居中,= 看下图)
sign:指定有无符号数(+、-、、空格)
width:指定输出数据时所占的宽度。
precision:指定保留的小数位数。
type:指定输出数据的具体类型
'''
哈?还看不懂吗?那我以例子为基础讲细一点
print("The dog is {0:0>2} years old,name {1}".format(1,"DaiMo"))
The dog is 01 years old,name DaiMo
大括号中第一个0表示位置参数,第二个0是在设定的字符长度下,空白处以0来填充。> 表示右对齐,2 表示字符长度为2.