ရိုးရိုးလေး ရှင်းပြမယ်
ရိုးရိုးလေး ရှင်းရရင်
String ဆိုတာ စာလုံးတွေကို အစဉ်လိုက် တန်းစီထားတဲ့ text ပါ။ နာမည်၊ email၊ message၊ file path စတာတွေ အားလုံး string တွေပါ။ Python မှာ text ကို 'single quote'၊ "double quote" ဒါမှမဟုတ် """triple quote""" ထဲ ထည့်ရေးရုံပါပဲ။
စာလုံးတိုင်းမှာ 0 ကနေ စတဲ့ နံပါတ် (index) ရှိလို့ "Python"[0] က "P" ဖြစ်ပါတယ်။ စာလုံး အပိုင်းအစ လိုချင်ရင် slice နဲ့ ဖြတ်ယူပြီး စာလုံးအကြီး ပြောင်းတာ၊ space ဖယ်တာလို အလုပ်တွေကို method တွေနဲ့ လုပ်ပါတယ်။
Problem → Why → Solution
ပြဿနာ: strip() ခေါ်ပြီးပြီ၊ ဒါပေမယ့် login မဝင်ဘူး
Login form မှာ user က " Aye " လို့ space တွေ ပါပြီး စာလုံးအကြီးနဲ့ ရိုက်ထည့်ပါတယ်။ Developer က strip() နဲ့ lower() ကို ခေါ်ထားပေမယ့် username က "aye" နဲ့ ဘယ်တော့မှ မတူပါဘူး။ Error မတက်လို့ ဘာမှားနေလဲ ရှာရခက်ပါတယ် — method တွေက အလုပ်လုပ်သွားပေမယ့် ရလဒ်ကို ဘယ်မှာမှ မသိမ်းခဲ့ပါဘူး။
username = " Aye "
username.strip() # result is discarded
username.lower() # result is discarded
print(username == "aye") # False
print(repr(username)) # ' Aye 'ဘာကြောင့် မပြောင်းသွားတာလဲ
Python string တွေက immutable ပါ — ဖန်တီးပြီးရင် အထဲက စာလုံးတွေကို ဘယ်တော့မှ ပြင်လို့ မရပါဘူး။ strip()၊ lower() လို method တိုင်းက မူရင်းကို ထိမထိဘဲ string အသစ်တစ်ခုကို return ပြန်ပါတယ်။ ဒါကြောင့် username = username.strip().lower() လို့ ရလဒ်ကို variable ထဲ ပြန် assign လုပ်မှသာ ပြောင်းလဲမှုကို ရပါမယ်။
Core concept: string က မပြောင်းနိုင်တဲ့ စာလုံး sequence ပါ
'Hello' နဲ့ "Hello" က အတူတူပါပဲ။ Double quote ကို စာထဲမှာ apostrophe ပါတဲ့အခါ ("It's fine") သုံးလေ့ရှိပြီး triple quote က line အများကြီး ပါတဲ့ text အတွက်ပါ။ String ထဲမှာ \n က line အသစ်၊ \t က tab ကို ကိုယ်စားပြုပါတယ်။
Indexes of "Python"
-------------------
+---+---+---+---+---+---+
| P | y | t | h | o | n |
+---+---+---+---+---+---+
0 1 2 3 4 5 positive index
-6 -5 -4 -3 -2 -1 negative index
text[0:3] -> "Pyt" (stop index 3 is NOT included)
text[2:] -> "thon" (from index 2 to the end)Index က 0 ကနေ စပြီး negative index က အနောက်ကနေ ရေတွက်ပါတယ်၊ -1 က နောက်ဆုံး စာလုံးပါ။ Slice text[start:stop] မှာ start ပါပြီး stop မပါပါဘူး၊ ဒါကြောင့် text[0:3] က စာလုံး 3 လုံး ရပါတယ်။ start ကို ချန်ရင် အစကနေ၊ stop ကို ချန်ရင် အဆုံးထိ ယူပါတယ်။ အရှည်ကျော်တဲ့ index တစ်ခုတည်း (text[10]) က IndexError ပေးပေမယ့် slice ကတော့ ရှိသလောက်ပဲ ပြန်ပေးပြီး error မတက်ပါဘူး။
String တွေက immutable ဖြစ်လို့ text[0] = "J" လို့ ရေးရင် TypeError: 'str' object does not support item assignment ဖြစ်ပါတယ်။ upper()၊ lower()၊ strip()၊ replace() တို့က string အသစ်ကို ပြန်ပေးပြီး split() ကတော့ list တစ်ခု ပြန်ပေးပါတယ်။ Argument မပါတဲ့ split() က space ဘယ်နှခုပဲ ရှိရှိ စကားလုံးတွေ ခွဲပေးပြီး အစနဲ့ အဆုံးက space တွေကိုလည်း လျစ်လျူရှုပါတယ်။
f"..." string ထဲက {} တွေမှာ variable ဒါမှမဟုတ် expression တွေ ထည့်လို့ရပြီး Python က ဘယ် type မဆို text အဖြစ် ပြောင်းပေးပါတယ်။ len() က စာလုံး အရေအတွက်ကို ပြန်ပေးပြီး "py" in "python" လို in operator က substring ပါမပါ စစ်ပါတယ်။
String တွေကို + နဲ့ ဆက်လို့ရပြီး * နဲ့ ထပ်လို့ရပါတယ် — "Py" + "thon" က "Python"၊ "ab" * 3 က "ababab" ပါ။ Python 3 ရဲ့ string တိုင်းက Unicode ဖြစ်လို့ မြန်မာစာ၊ emoji တွေကို ပြဿနာမရှိ သိမ်းနိုင်ပါတယ်။ သတိထားရမှာက len() က မျက်စိနဲ့ မြင်ရတဲ့ စာလုံးကို မရေတွက်ဘဲ Unicode code point တွေကို ရေတွက်တာပါ။ ဥပမာ "မြန်မာ" ကို စာလုံး သုံးလုံးလို့ မြင်ရပေမယ့် ဗျည်း၊ သရနဲ့ အသတ်တွေ ပေါင်းပြီး len() က 6 ပြန်ပေးပါတယ်။
- Index 0 ကနေ စ၊ -1 က နောက်ဆုံး
- Slice က start ပါ၊ stop မပါ
- Method က string အသစ် ပြန်ပေး
- ရလဒ်ကို variable ထဲ ပြန်သိမ်း
String ကို ဘယ်တော့မှ နေရာမှာတင် ပြင်လို့ မရပါဘူး၊ ပြင်ထားတဲ့ မိတ္တူကို ပြန်ရတာပါ။
အထွေထွေ ပုံစံ
text = "some text"
char = text[index] # one character, index from 0
part = text[start:stop] # stop is excluded
last = text[-1] # count from the end
new_text = text.method(args) # methods return a NEW string
words = text.split() # -> list of words
message = f"{name} is {age} years old"
size = len(text)Index နဲ့ slice က စာလုံးတွေ ဖတ်ဖို့၊ method တွေက string အသစ် ထုတ်ဖို့၊ f-string က variable တွေ ထည့်ဖို့ပါ။ Method ရလဒ်ကို new_text လို variable အသစ် ဒါမှမဟုတ် မူရင်း variable ထဲ ပြန်သိမ်းရပါတယ်။
Example: ဖန်တီး၊ ဖြတ်၊ ပြောင်း၊ format လုပ်
ဒီ example က quote သုံးမျိုးနဲ့ string ဖန်တီးပြီး "Python" ကို index နဲ့ slice လုပ်ပါတယ်။ ပြီးရင် space ပါတဲ့ " hello world " ပေါ်မှာ method ငါးခု စမ်းပါတယ် — output မှာ upper() နဲ့ replace() က အစက space တွေကို ဆက်ထားပြီး strip() ကပဲ ဖယ်ပေးတာကို သတိထားကြည့်ပါ။
# ===== 1. String Creation =====
a = 'Hello'
b = "World"
c = """This is
a multi-line
string"""
print(f"Single quotes: {a}")
print(f"Double quotes: {b}")
print(f"Multi-line: {c}")
# ===== 2. String Indexing & Slicing =====
print("\n===== Indexing & Slicing =====")
text = "Python"
print(f"First character: {text[0]}") # P
print(f"Last character: {text[-1]}") # n
print(f"Slice [0:3]: {text[0:3]}") # Pyt
print(f"Slice [2:]: {text[2:]}") # thon
# ===== 3. String Methods =====
print("\n===== String Methods =====")
txt = " hello world "
print(f"Upper: {txt.upper()}")
print(f"Lower: {txt.lower()}")
print(f"Strip: '{txt.strip()}'")
print(f"Replace: {txt.replace('world', 'Python')}")
print(f"Split: {txt.split()}")
# ===== 4. String Formatting (f-strings) =====
print("\n===== String Formatting =====")
name = "Sai"
age = 25
print(f"My name is {name}, I am {age} years old.")
# ===== 5. String Length =====
print(f"\nLength of 'Python': {len('Python')}")Code ကို တစ်ပိုင်းချင်း ရှင်းပြမယ်
- Lines 4-6
c = """This isTriple quote က Enter ခေါက်ထားတဲ့ line break တွေကို string ထဲ ထည့်သိမ်းလို့ print လုပ်တဲ့အခါ line သုံးကြောင်း ထွက်ပါတယ်။
- Line 16
print(f"Last character: {text[-1]}") # n-1 က အနောက်ဆုံး စာလုံးပါ၊ text[len(text) - 1] လို့ ရေးစရာ မလိုပါဘူး။
- Line 17
print(f"Slice [0:3]: {text[0:3]}") # PytIndex 0၊ 1၊ 2 ကို ယူပြီး 3 မှာ ရပ်လို့ "Pyt" ရပါတယ်။ စာလုံး အရေအတွက်က stop - start = 3 ပါ။
- Line 25
print(f"Strip: '{txt.strip()}'")Quote တွေကြားမှာ ပြထားလို့ အစနဲ့ အဆုံးက space တွေ ပျောက်သွားတာကို မြင်ရပါတယ်။ txt ကိုယ်တိုင်ကတော့ မပြောင်းပါဘူး။
- Line 27
print(f"Split: {txt.split()}")Argument မပါတဲ့ split() က whitespace နေရာတိုင်းမှာ ခွဲပြီး ဗလာ string တွေ မပါတဲ့ ['hello', 'world'] list ကို ပြန်ပေးပါတယ်။
Single quotes: Hello
Double quotes: World
Multi-line: This is
a multi-line
string
===== Indexing & Slicing =====
First character: P
Last character: n
Slice [0:3]: Pyt
Slice [2:]: thon
===== String Methods =====
Upper: HELLO WORLD
Lower: hello world
Strip: 'hello world'
Replace: hello Python
Split: ['hello', 'world']
===== String Formatting =====
My name is Sai, I am 25 years old.
Length of 'Python': 6Step ပါတဲ့ slice နဲ့ စစ်ဆေးတဲ့ method များ
Slice မှာ တတိယ တန်ဖိုး step ကို ထည့်လို့ရပါတယ် — text[::2] က တစ်လုံးကျော်စီ ယူပြီး text[::-1] က string ကို ပြောင်းပြန် လှန်ပေးပါတယ်။ String ကို ပြင်တဲ့ method တွေအပြင် မေးခွန်းမေးတဲ့ method တွေလည်း ရှိပါတယ်။ startswith()၊ endswith()၊ isdigit() တို့က True/False ပြန်ပြီး find() က substring ရဲ့ index ကို (မတွေ့ရင် -1)၊ count() က ဘယ်နှကြိမ် ပါလဲ ပြန်ပေးပါတယ်။ ဆန့်ကျင်ဘက် split() ကတော့ "-".join(list) ပါ၊ list ထဲက string တွေကို ကြားမှာ separator ထည့်ပြီး ဆက်ပေးပါတယ်။
text = "Python"
print(text[::2]) # Pto
print(text[::-1]) # nohtyP
print("report.pdf".endswith(".pdf")) # True
print("2024".isdigit()) # True
print("banana".find("na"), "banana".count("a")) # 2 3
print("-".join(["2024", "05", "17"])) # 2024-05-17Escape character နဲ့ raw string
Backslash (\) က နောက်က စာလုံးနဲ့ ပေါင်းပြီး အထူး အဓိပ္ပာယ် ဖြစ်စေပါတယ် — \n က line အသစ်၊ \t က tab၊ \' က quote ပါ။ ဒါကြောင့် Windows path "C:\new\table" ကို print လုပ်ရင် \n နဲ့ \t က line break နဲ့ tab ဖြစ်သွားပြီး path ပျက်သွားပါတယ်။ String ရှေ့မှာ r ထည့်ထားတဲ့ raw string r"C:\new\table" က backslash တွေကို စာလုံး အတိုင်း ထားပေးလို့ file path နဲ့ regular expression တွေမှာ သုံးပါတယ်။
လက်တွေ့ project တွေမှာ
Real project တွေမှာ ဘယ်လို သုံးလဲ
User input ကို သိမ်းခင် သန့်ရှင်းအောင် လုပ်တာက string method တွေရဲ့ အဓိက အလုပ်ပါ။ Email ကို email.strip().lower() နဲ့ normalize လုပ်ပြီးမှ database မှာ ရှာလို့ "Aye@Mail.com " နဲ့ "aye@mail.com" က user တစ်ယောက်တည်း ဖြစ်ပါတယ်။ Blog တွေမှာ title ကို lower() နဲ့ replace(" ", "-") သုံးပြီး URL slug လုပ်ပါတယ်။
Log file တွေနဲ့ CSV line တွေကို line.split(",") နဲ့ ခွဲပြီး field တွေ ထုတ်ယူပါတယ်။ Slicing ကိုတော့ ဖုန်းနံပါတ် ဒါမှမဟုတ် card နံပါတ်ရဲ့ နောက်ဆုံး ဂဏန်း လေးလုံးကိုပဲ ပြပြီး ကျန်တာကို * နဲ့ ဖုံးတဲ့အခါ၊ file name ကနေ extension ထုတ်တဲ့အခါ သုံးပါတယ်။ Message၊ email template နဲ့ log line တိုင်းနီးပါးကိုတော့ f-string နဲ့ တည်ဆောက်ပါတယ်။ Search box တွေကလည်း user ရိုက်ထည့်တဲ့ စာက product နာမည်ထဲ ပါမပါ စစ်ဖို့ in operator ဒါမှမဟုတ် find() ကို အားကိုးပါတယ်။
အမှားများ
အဖြစ်များတဲ့ အမှားတွေ
1. Method ရလဒ်ကို မသိမ်းတာ
name.upper() တစ်ကြောင်းတည်း ရေးထားရင် string အသစ် ဖန်တီးပြီး ချက်ချင်း လွှင့်ပစ်လိုက်တာပါ။ name = name.upper() လို့ ပြန် assign လုပ်ပါ။ List method တွေလို (append) နေရာမှာတင် ပြင်မယ်လို့ ထင်တာက ဒီအမှားရဲ့ အရင်းအမြစ်ပါ။
2. Slice ရဲ့ stop ကို ပါမယ်လို့ ထင်တာ
"Python"[1:3] က "yth" မဟုတ်ဘဲ "yt" ပါ။ စာလုံး n လုံး လိုချင်ရင် text[start:start + n] လို့ တွေးပါ၊ ဒါမှ off-by-one အမှား မဖြစ်ပါဘူး။ Slice အရှည်က stop နုတ် start နဲ့ အမြဲ ညီတယ်ဆိုတာ မြန်မြန် စစ်ကြည့်လို့ရတဲ့ နည်းပါ။
3. string နဲ့ number ကို + နဲ့ ဆက်တာ
"Age: " + 25 က TypeError: can only concatenate str (not "int") to str ဖြစ်ပါတယ်။ f"Age: {age}" ကို သုံးရင် conversion ကို f-string က လုပ်ပေးလို့ ဒီ error ကို လုံးဝ ရှောင်လို့ ရပါတယ်။
Best Practices
အကောင်းဆုံး အလေ့အကျင့်များ
- Text ထဲ value တွေ ထည့်ဖို့ f-string ကို default အဖြစ် သုံးပါ။
- User input ကို နှိုင်းယှဉ်ခင် strip() နဲ့ lower() (ဒါမှမဟုတ် casefold()) လုပ်ပါ။
- Debug လုပ်တဲ့အခါ print(repr(text)) နဲ့ မမြင်ရတဲ့ space နဲ့ \n တွေကို ကြည့်ပါ။
Mini challenge
Mini challenge: user profile text helper များ
Sign-up form ကနေ ရတဲ့ နာမည်နဲ့ ဖုန်းနံပါတ်ကို သန့်ရှင်းအောင် လုပ်ပြီး username ထုတ်ပေးတဲ့ function သုံးခု ရေးပါ။
- clean_name(raw) က space အပိုတွေ ဖယ်ပြီး စကားလုံးတိုင်းကို စာလုံးအကြီးနဲ့ စရမယ် (" aung ko ko " → "Aung Ko Ko")
- make_username(full_name) က စာလုံးအသေးနဲ့ _ ခံပြီး ဆက်ရမယ် ("aung_ko_ko")
- mask_phone(phone) က နောက်ဆုံး ဂဏန်း 4 လုံးပဲ ပြပြီး ကျန်တာကို * နဲ့ ဖုံးရမယ်
- မူရင်း raw string မပြောင်းကြောင်း print ထုတ်ပြရမယ်
def clean_name(raw: str) -> str:
# TODO: split on whitespace, join with one space, then .title()
...
def make_username(full_name: str) -> str:
# TODO: lowercase the words and join them with "_"
...
def mask_phone(phone: str) -> str:
# TODO: "*" repeated (len - 4) times + the last 4 characters
...
raw = " aung ko ko "
# TODO: print the cleaned name in quotes, the username and the first name
# TODO: print mask_phone("09123456789")
# TODO: print whether the name starts with "Aung" and that raw is unchangedရလဒ်: Program က အောက်ပါအတိုင်း ထုတ်ပေးရပါမယ်: Name: 'Aung Ko Ko' Username: aung_ko_ko First name: Aung Phone: *******6789 Starts with 'Aung': True ' aung ko ko ' unchanged: True
အရိပ်အမြွက် ကြည့်ရန်
" ".join(raw.split()) က space အပိုတွေကို တစ်ခုတည်း ဖြစ်အောင် လုပ်ပေးပါတယ်။ "*" * 7 က "*******" ဖြစ်ပြီး phone[-4:] က နောက်ဆုံး စာလုံး လေးလုံးပါ။
အဖြေ ကြည့်ရန်
def clean_name(raw: str) -> str:
"""Remove extra spaces and capitalise each word."""
return " ".join(raw.split()).title()
def make_username(full_name: str) -> str:
"""'Aung Ko Ko' -> 'aung_ko_ko'"""
return "_".join(full_name.lower().split())
def mask_phone(phone: str) -> str:
"""Keep only the last 4 digits visible."""
return "*" * (len(phone) - 4) + phone[-4:]
raw = " aung ko ko "
name = clean_name(raw)
print(f"Name: '{name}'")
print(f"Username: {make_username(name)}")
print(f"First name: {name.split()[0]}")
print(f"Phone: {mask_phone('09123456789')}")
print(f"Starts with 'Aung': {name.startswith('Aung')}")
print(f"'{raw}' unchanged: {raw == ' aung ko ko '}")Quick Quiz 1 / 3
Quick Quiz 2 / 3
Quick Quiz 3 / 3
မှတ်ထားရမယ့် အချက်များ
- String က index 0 ကနေ စတဲ့ စာလုံး sequence ဖြစ်ပြီး -1 က နောက်ဆုံး စာလုံးပါ။
- Slice [start:stop] မှာ stop မပါဘဲ၊ index တစ်ခုတည်း ကျော်ရင် IndexError ဖြစ်ပါတယ်။
- String တွေက immutable ဖြစ်လို့ method တိုင်းက string အသစ် ပြန်ပေးပြီး ရလဒ်ကို ပြန်သိမ်းရပါတယ်။
- Text ထဲ value ထည့်ဖို့ f-string ကို သုံးပြီး len() နဲ့ in နဲ့ စစ်ဆေးပါ။
The Python Tutorial — Python Software Foundation