Python - ဖော်မတ် - စာကြောင်းများ
စာတန်းပုံစံ
Python Variables အခန်းတွင် လေ့လာခဲ့သည့်အတိုင်း၊ ဤကဲ့သို့သော စာကြောင်းများနှင့် နံပါတ်များကို ပေါင်းစပ်၍မရပါ။
ဥပမာ
age = 36
txt = "My name is John, I am " + age
print(txt)
ဒါပေမယ့် format()
နည်းလမ်းကိုသုံးပြီး ကြိုးတွေနဲ့ နံပါတ်တွေကို ပေါင်းစပ်နိုင်ပါတယ်။
နည်းလမ်းသည် လွန်ခဲ့သည့် အကြောင်းပြချက်များကို ယူဆောင်ကာ ၎င်းတို့ကို format()
ဖော်မတ်ထုတ်ကာ နေရာယူထားသည့် စာသားနေရာတွင် ၎င်းတို့ကို နေရာချ
{}
သည်-
ဥပမာ
format()
ဂဏန်းများကို စာကြောင်းများထဲသို့ ထည့်ရန် နည်းလမ်းကို အသုံးပြု ပါ
age = 36
txt = "My name is John, and I am {}"
print(txt.format(age))
ဖော်မတ်() နည်းလမ်းသည် အကြောင်းပြချက်များ၏ အကန့်အသတ်မရှိ အရေအတွက်ကို ယူကာ သက်ဆိုင်ရာ နေရာကိုင်ဆောင်သူများထံ ထားရှိပေးသည်-
ဥပမာ
quantity = 3
itemno = 567
price = 49.95
myorder = "I want {}
pieces of item {} for {} dollars."
print(myorder.format(quantity,
itemno, price))
{0}
အကြောင်းပြချက်များကို မှန်ကန်သောနေရာကိုင်ဆောင်သူများတွင် ထားရှိကြောင်းသေချာစေရန် အညွှန်းနံပါတ်များကို သင်အသုံးပြုနိုင် သည်-
ဥပမာ
quantity = 3
itemno = 567
price = 49.95
myorder = "I want to pay {2}
dollars for {0} pieces of item {1}."
print(myorder.format(quantity,
itemno, price))
ကျွန်ုပ်တို့၏ String Formatting အခန်း တွင် String Formatting အကြောင်း ပိုမိုလေ့လာပါ ။