Python String split() နည်းလမ်း
ဥပမာ
စကားလုံးတစ်ခုစီသည် စာရင်းတစ်ခုပါရှိသော စာရင်းတစ်ခုသို့ စာကြောင်းတစ်ခု ခွဲလိုက်ပါ-
txt = "welcome to the jungle"
x = txt.split()
print(x)
အဓိပ္ပါယ်နှင့် အသုံးပြုမှု
နည်းလမ်း သည် split()
စာကြောင်းတစ်ခုကို စာရင်းတစ်ခုအဖြစ် ပိုင်းခြားသည်။
ခြားနားချက်ကို သင် သတ်မှတ်နိုင်သည်၊ ပုံသေ ခြားနားချက်သည် မည်သည့် နေရာလွတ်မဆို ဖြစ်သည်။
မှတ်ချက် ။ _ _
အထားအသို
string.split(separator, maxsplit)
ကန့်သတ်တန်ဖိုးများ
Parameter | Description |
---|---|
separator | Optional. Specifies the separator to use when splitting the string. By default any whitespace is a separator |
maxsplit | Optional. Specifies how many splits to do. Default value is -1, which is "all occurrences" |
နောက်ထပ် ဥပမာများ
ဥပမာ
ကော်မာကို အသုံးပြု၍ မျဉ်းကြောင်းကို ခွဲပါ၊ ခြားနားချက်အဖြစ် နေရာလွတ်တစ်ခုဖြင့် ခွဲပါ-
txt = "hello, my name is Peter, I am 26 years old"
x = txt.split(", ")
print(x)
ဥပမာ
ခြားနားချက်အဖြစ် hash စာလုံးကို သုံးပါ-
txt = "apple#banana#cherry#orange"
x = txt.split("#")
print(x)
ဥပမာ
အများဆုံး 2 ခုပါရှိသော string ကို စာရင်းတစ်ခုသို့ ခွဲပါ-
txt = "apple#banana#cherry#orange"
# setting the maxsplit parameter
to 1, will return a list with 2 elements!
x = txt.split("#", 1)
print(x)