1. 程式人生 > >字符串format拼接格式化

字符串format拼接格式化

res 一個 劉鵬 字符串 var form col spa 拼接

# ###字符串的格式化 format
"""
(1)順序傳參
(2)索引傳參
(3)關鍵字傳參
(4)容器類型傳參(列表和元組)

{} 相當於占位符
"""

# (1) 順序傳參
strvar = "{}向{}開了一槍,飲彈而忙"
res = strvar.format(" 鐘立文","劉鵬")
print(res)


# (2) 索引傳參
strvar = "{1}給{0}一個大大的擁抱,幸福溫暖"
res = strvar.format("徐欣欣","鄭滿")
print(res)

# (3) 關鍵字傳參
strvar = "{who1}向{who2}進行掃射,渾身都是彈孔"
res = strvar.format(who1="林誌遠",who2="中林誌")
print(res)

# (4) 容器類型傳參
# (1)
strvar = "{0[2]}給了{1[0]} 拋來一個媚眼,鼻血直冒3000多尺"
res = strvar.format(["鐘立文","趙成","胡斌"],("徐欣欣","胡啟超","胡家豪"))
print(res)

# (2) format 格式化時, 要註意如果是字典,不要加引號.

strvar = "{group2[zlw]} 給{group1[xxx]} 拋了一個媚眼,鼻血直冒3萬多尺"
res = strvar.format(group1 = {"xxx":"徐欣欣","hqc":"胡啟超"},group2 ={"zlw":"鐘立文","dmx":"戴明雪"})
print(res)




























# ###字符串的格式化 format
"""
(1)順序傳參
(2)索引傳參
(3)關鍵字傳參
(4)容器類型傳參(列表和元組)

{} 相當於占位符
"""

# (1) 順序傳參
strvar = "{}向{}開了一槍,飲彈而忙"
res = strvar.format(" 鐘立文","劉鵬")
print(res)


# (2) 索引傳參
strvar = "{1}給{0}一個大大的擁抱,幸福溫暖"
res = strvar.format("徐欣欣","鄭滿")
print(res)

# (3) 關鍵字傳參
strvar = "
{who1}向{who2}進行掃射,渾身都是彈孔" res = strvar.format(who1="林誌遠",who2="中林誌") print(res) # (4) 容器類型傳參 # (1) strvar = "{0[2]}給了{1[0]} 拋來一個媚眼,鼻血直冒3000多尺" res = strvar.format(["鐘立文","趙成","胡斌"],("徐欣欣","胡啟超","胡家豪")) print(res) # (2) format 格式化時, 要註意如果是字典,不要加引號. strvar = "{group2[zlw]} 給{group1[xxx]} 拋了一個媚眼,鼻血直冒3萬多尺" res = strvar.format(group1 = {"xxx":"徐欣欣","hqc":"胡啟超"},group2 ={"zlw":"鐘立文","dmx":"戴明雪"}) print(res)

字符串format拼接格式化