Python Strings

String (str) ဆိုတာ ဘာလဲ?

ရှင်းပါတယ်။ String ဆိုတာ စာသား (Text) တွေ။

bro ရဲ့နာမည်၊ လိပ်စာ၊ Hello World တို့၊ ထမင်းစားပြီးပြီလား တို့၊ အဲ့ဒါတွေအကုန် String တွေပဲ။ Python က ဒါကို data type တစ်ခုအနေနဲ့ str လို့ ခေါ်တယ်။

String တစ်ခု ဆောက် ချင်ရင်၊ bro ရဲ့ စာသားတွေကို Single Quote (') ဒါမှမဟုတ် Double Quote (") နှစ်ခု ထဲက တစ်ခုနဲ့ ညှပ် ပြီး ရေးရတယ်။

ဥပမာ:

strings_basic.py
# Double quote နဲ့ name = "Mg Mg" # Single quote နဲ့ greeting = 'Hello Bro!' # နှစ်မျိုးလုံး တူတူ ပဲ။ print(name) print(greeting)
?: ဘာလို့ Quote နှစ်မျိုး ပေးထားတာလဲ? bro ရဲ့စာသားထဲမှာ ' (single quote) ပါနေရင်၊ အပြင်ကနေ " (double quote) နဲ့ အုပ် လိုက်။ ဥပမာ: sentence = "It's a sunny day." အဲ့လို ရောချလို့ရတယ်။ အဆင်ပြေတယ်။

String တွေကို ပေါင်းစပ်ခြင်း (Concatenation)

String တွေကို ဆက် ချင်ရင် + (အပေါင်း) လက္ခဏာ သုံးရုံပဲ။ လွယ်တယ်။

concatenation.py
first_name = "Kyaw" last_name = "Gyi" # ကြားထဲမှာ Space (နေရာလွတ်) လေး ခံ ပေးဖို့ မမေ့နဲ့။ full_name = first_name + " " + last_name print(full_name) # Output: "Kyaw Gyi"

အလန်းစား f-Strings

ဒါက ခေတ်သစ် Python မှာ String တွေနဲ့ variable (ကိန်း) တွေကို ရော ဖို့ အလွယ်ဆုံး နည်းပဲ။ သုံးရတာလန်းတယ်။

String ရဲ့ အရှေ့ဆုံးမှာ f လေး ထည့်လိုက်။ ပြီးရင် bro ထည့်ချင်တဲ့ variable ကို {} (curly braces) ထဲ ထည့်ရုံပဲ။

f_strings.py
name = "မိချော" age = 3 # f-string သုံးမယ်... output = f"ငါ့နာမည်က {name}။ ငါ့အသက် {age} နှစ်။" print(output) # Output: "ငါ့နာမည်က မိချော။ ငါ့အသက် 3 နှစ်။"
အကြံပြု: ဘယ်လိုလဲ? အပေါင်း + တွေနဲ့ ထပ်တလဲလဲ ပေါင်းနေတာထက် အများကြီး ပိုရှင်းတယ်။ ဒါကိုကျွမ်းအောင်သုံး။

String Operations

String မှာ Built-in ပါတဲ့ methods တွေ အများကြီးရှိတယ်။ bro ရဲ့စာသားကို ကြိုက်သလို ကစားလို့ရတယ်။

my_text = " Hello, Welcome to Python! " ဆိုတဲ့ String တစ်ခုကို စမ်းကြည့်မယ်။

Case ပြောင်းတာ (Changing Case):

.upper(): စာလုံး အကြီး တွေချည်း ဖြစ်သွားမယ်။

upper_method.py
my_text = " Hello, Welcome to Python! " print(my_text.upper()) # Output: " HELLO, WELCOME TO PYTHON! "

.lower(): စာလုံး အသေး တွေချည်း ဖြစ်သွားမယ်။

lower_method.py
print(my_text.lower()) # Output: " hello, welcome to python! "

အပို whitespace ဖယ်မယ်:

.strip(): ဘေးနှစ်ဖက် က မလိုတဲ့ Space အလွတ်တွေကို ဖြုတ်တယ်။ Data တွေကိုင်ရင် အရမ်းသုံးရတယ်။

strip_method.py
my_text = " Hello, Welcome to Python! " cleaned_text = my_text.strip() print(cleaned_text) # Output: "Hello, Welcome to Python!"

အစားထိုးတာ (Replacing):

.replace("ဒါကို", "ဒါနဲ့"): စာလုံးတစ်လုံး နေရာမှာ နောက်တစ်လုံး နဲ့ ဝင်ထိုးတာ။

replace_method.py
cleaned_text = "Hello, Welcome to Python!" print(cleaned_text.replace("Python", "Our World")) # Output: "Hello, Welcome to Our World!"

ခွဲထုတ်တာ (Splitting):

.split("ဒီစာလုံးတွေ့တိုင်း"): စာသားတန်း ရှည်ကြီး ကို အပိုင်းပိုင်း တွေခွဲပြီး List တစ်ခု တန်းဆောက်ပေးတယ်။

split_method.py
sentence = "ငါ,bro,မိချော,မုန့်လုံး" names_list = sentence.split(",") # Comma (,) တွေ့တိုင်း ဖြတ်မယ် print(names_list) # Output: ['ငါ', 'bro', 'မိချော', 'မုန့်လုံး']

စစ်ဆေးတာ (Checking):

.startswith("..."): ဒီစာလုံးနဲ့ စလားဆိုတာစစ်တယ်။ (True/False ပြန်ပေးတယ်)

.endswith("..."): ဒီစာလုံးနဲ့ ဆုံးလားဆိုတာစစ်တယ်။

checking_methods.py
cleaned_text = "Hello, Welcome to Python!" print(cleaned_text.startswith("Hello")) # True print(cleaned_text.endswith("!")) # True print(cleaned_text.startswith("Bye")) # False

အပိုင်းအစ ယူတာ (Indexing & Slicing) - ဒါလည်း အသက်

String ထဲက စာလုံးတစ်လုံးချင်း (Indexing) ဖြစ်ဖြစ်၊ အပိုင်းလိုက် (Slicing) ဖြစ်ဖြစ် ဖြတ်ယူလို့ရတယ်။

မှတ်ရမယ့်အချက်: Python မှာ အမြဲတမ်း 0 (သုည) ကနေ စ ရေတွက်တယ်။

s = "Python" ဆိုတဲ့ string ကို ခွဲ ကြည့်မယ်။

string_index.py
# P y t h o n # 0 1 2 3 4 5

Indexing (တစ်လုံးချင်း ယူ):

indexing.py
s = "Python" print(s[0]) # 'P' (ပထမဆုံး ကောင်) print(s[2]) # 't' print(s[-1]) # 'n' (အနှုတ် ဆို နောက်ဆုံး ကနေရေတွက်ရမယ်။) print(s[-2]) # 'o'

Slicing (အပိုင်းလိုက် ဖြတ်ယူ): [start:stop]

start နေရာက စမယ်။ stop နေရာ မရောက်ခင်မှာ ရပ်မယ်။

slicing.py
s = "Python" print(s[0:2]) # 'Py' (0 ကနေ စ၊ 2 မရောက်ခင် (1) မှာ ရပ်) print(s[2:5]) # 'tho' print(s[1:]) # 'ython' (1 ကနေ အဆုံးထိ) print(s[:4]) # 'Pyth' (အစ ကနေ 4 မရောက်ခင်အထိ) print(s[:]) # 'Python' (အစကနေ အဆုံးထိ အကုန် ပြန်ယူတာ)

သတိ (Immutability)

String မှာ အရေးကြီးတဲ့ သဘောတရားတစ်ခု ရှိတယ်။ String တွေက Immutable (ပြင်လို့မရဘူး)။

ဘာလဲ ဆိုတော့... bro မှာ name = "Sam" ရှိတယ် ဆိုပါစို့။ bro က S နေရာမှာ P ကို တိုက်ရိုက် ပြောင်းပြီး Pam ဖြစ်အောင် လုပ်လို့ မရဘူး။

immutability_error.py
name = "Sam" # name[0] = "P" <-- ဒီလို တန်းလုပ်လို့ မရဘူး။ Error တက်လိမ့်မယ်။
ဒါဆို ဘယ်လိုလုပ်မလဲ? bro အသစ်တစ်ခု ပြန်ဆောက်ရမယ်။
immutability_solution.py
name = "Sam" # Slicing နဲ့ ပေါင်းစပ်တာကို သုံးမယ် new_name = "P" + name[1:] # "P" ကို name ရဲ့ 1 ကနေ အဆုံးထိ ("am") နဲ့ ပေါင်း print(new_name) # Output: "Pam"
မှတ်ချက်: ဒါက အသစ် တစ်ခု ဖန်တီး လိုက်တာ။ ရှိပြီးသား name ကို တိုက်ရိုက် ပြင်လိုက်တာ မဟုတ်ဘူး။ ဒီအချက်ကို မမေ့နဲ့။

1. Case Conversion Methods (စာလုံးအကြီးအသေး ပြောင်းတာ)

.upper() - စာလုံးအကြီးတွေ ဖြစ်အောင်

စာသား တစ်ခုလုံးကို စာလုံးကြီးတွေ ဖြစ်အောင် ပြောင်းတယ်။

upper_example.py
text = "hello world" result = text.upper() print(result) # Output: "HELLO WORLD" # Practical Example: အမည် စာရင်းမှာ တူညီအောင် ရှာတာ user_input = "mgmg" database_name = "MGMG" if user_input.upper() == database_name: print("အမည် တူညီတယ်")

.lower() - စာလုံးအသေးတွေ ဖြစ်အောင်

စာသား တစ်ခုလုံးကို စာလုံးသေးတွေ ဖြစ်အောင် ပြောင်းတယ်။

lower_example.py
text = "HELLO WORLD" result = text.lower() print(result) # Output: "hello world" # Practical Example: Email လိပ်စာ သိမ်းတာ email = "USER@EXAMPLE.COM" normalized_email = email.lower() print(normalized_email) # Output: "user@example.com"

.capitalize() - ပထမစာလုံးကို အကြီး ပြောင်းတာ

စာသားရဲ့ ပထမစာလုံးကိုပဲ အကြီး ပြောင်းပြီး ကျန်တာကို အသေး ပြောင်းတယ်။

capitalize_example.py
text = "hello world" result = text.capitalize() print(result) # Output: "Hello world" # Practical Example: စာကြောင်း စတာ sentence = "python is awesome" print(sentence.capitalize()) # Output: "Python is awesome"

.title() - စကားလုံးတိုင်းရဲ့ ပထမစာလုံးကို အကြီး

စကားလုံးတစ်လုံးချင်းစီရဲ့ ပထမစာလုံးကို အကြီး ပြောင်းတယ်။

title_example.py
text = "hello world python" result = text.title() print(result) # Output: "Hello World Python" # Practical Example: စာအုပ် ခေါင်းစဉ် book_title = "learn python programming" print(book_title.title()) # Output: "Learn Python Programming"

.swapcase() - အကြီးအသေး လှန်ပြောင်းတာ

အကြီးကို အသေး၊ အသေးကို အကြီး ပြောင်းတယ်။

swapcase_example.py
text = "Hello World" result = text.swapcase() print(result) # Output: "hELLO wORLD"

2. Whitespace Methods (Space ဖယ်တာ)

.strip() - နှစ်ဖက်က Space ဖြုတ်တာ

စာသားရဲ့ အစနဲ့ အဆုံးက Space တွေကို ဖယ်ရှားတယ်။

strip_example.py
text = " Hello World " result = text.strip() print(f"'{result}'") # Output: 'Hello World' # Practical Example: User input သန့်ရှင်းရေး username = input("Enter username: ") # user က " mgmg " လို့ ရိုက်ရင် clean_username = username.strip() print(f"Welcome {clean_username}")

.lstrip() - ဘယ်ဖက်က Space ဖြုတ်တာ

စာသားရဲ့ ဘယ်ဖက် (အစ) က Space တွေကိုပဲ ဖယ်ရှားတယ်။

lstrip_example.py
text = " Hello World " result = text.lstrip() print(f"'{result}'") # Output: 'Hello World ' (ညာဖက် Space ရှိနေတုန်း)

.rstrip() - ညာဖက်က Space ဖြုတ်တာ

စာသားရဲ့ ညာဖက် (အဆုံး) က Space တွေကိုပဲ ဖယ်ရှားတယ်။

rstrip_example.py
text = " Hello World " result = text.rstrip() print(f"'{result}'") # Output: ' Hello World' (ဘယ်ဖက် Space ကျန်နေ)

3. Search Methods (ရှာဖွေတာ)

.find() - စာလုံး ရှာတာ

ရှာတဲ့ စာလုံး ပထမဆုံး တွေ့ရာ index ကို ပြန်ပေးတယ်။ မတွေ့ရင် -1 ပြန်ပေးတယ်။

find_example.py
text = "Hello World" position = text.find("World") print(position) # Output: 6 not_found = text.find("Python") print(not_found) # Output: -1 # Practical Example: Email စစ်ဆေးတာ email = "user@example.com" if email.find("@") != -1: print("Valid email format")

.index() - စာလုံး ရှာတာ (find နဲ့ဆင်တယ်)

find() နဲ့ တူတယ်။ ဒါပေမယ့် မတွေ့ရင် Error တက်တယ်။

index_example.py
text = "Hello World" position = text.index("World") print(position) # Output: 6 # မတွေ့ရင် Error တက်မယ် # position = text.index("Python") # ValueError ထွက်မယ်
find() vs index(): find() က မတွေ့ရင် -1 ပြန်ပေးတယ်။ index() က မတွေ့ရင် Error တက်တယ်။ ဘရို စိတ်ချချင်ရင် find() သုံး။

.count() - ဘယ်နှခါ ပါသလဲ ရေတွက်တာ

ရှာတဲ့ စာလုံးက စာသားထဲမှာ ဘယ်နှကြိမ် ပါသလဲ ရေတွက်ပေးတယ်။

count_example.py
text = "Python is fun and Python is powerful" count = text.count("Python") print(count) # Output: 2 # Practical Example: စာလုံး အရေအတွက် sentence = "apple apple banana apple" apple_count = sentence.count("apple") print(f"apple ပါတာ {apple_count} ကြိမ်") # Output: apple ပါတာ 3 ကြိမ်

4. Checking Methods (စစ်ဆေးတာ)

.startswith() - ဒီစာလုံးနဲ့ စလား

စာသားက သတ်မှတ်ထားတဲ့ စာလုံးနဲ့ စလား စစ်တယ်။ True/False ပြန်ပေးတယ်။

startswith_example.py
text = "Hello World" print(text.startswith("Hello")) # True print(text.startswith("World")) # False # Practical Example: File extension စစ်တာ filename = "document.pdf" if filename.endswith(".pdf"): print("PDF ဖိုင်ပါ")

.endswith() - ဒီစာလုံးနဲ့ ဆုံးလား

စာသားက သတ်မှတ်ထားတဲ့ စာလုံးနဲ့ ဆုံးလား စစ်တယ်။

endswith_example.py
text = "Hello World" print(text.endswith("World")) # True print(text.endswith("Hello")) # False # Practical Example: URL စစ်တာ url = "https://example.com" if url.startswith("https://"): print("Secure connection")

.isdigit() - ဂဏန်းတွေချည်းလား

စာသားမှာ ဂဏန်းတွေချည်းပဲ ပါလား စစ်တယ်။

isdigit_example.py
print("12345".isdigit()) # True print("123abc".isdigit()) # False print("Hello".isdigit()) # False # Practical Example: အသက် စစ်တာ age_input = input("အသက် ထည့်ပါ: ") if age_input.isdigit(): age = int(age_input) print(f"အသက် {age} နှစ်") else: print("ဂဏန်း ထည့်ပါ")

.isalpha() - စာလုံးတွေချည်းလား

စာသားမှာ စာလုံးတွေချည်းပဲ ပါလား စစ်တယ်။

isalpha_example.py
print("Hello".isalpha()) # True print("Hello123".isalpha()) # False print("Hello World".isalpha())# False (Space ပါလို့) # Practical Example: အမည် စစ်တာ name = input("အမည် ထည့်ပါ: ") if name.isalpha(): print(f"ကြိုဆိုပါတယ် {name}") else: print("စာလုံးတွေချည်း ထည့်ပါ")

.isalnum() - စာလုံး သို့မဟုတ် ဂဏန်းလား

စာသားမှာ စာလုံး သို့မဟုတ် ဂဏန်းတွေပဲ ပါလား စစ်တယ်။

isalnum_example.py
print("Hello123".isalnum()) # True print("Hello 123".isalnum()) # False (Space ပါလို့) print("Hello@123".isalnum())# False (@ ပါလို့) # Practical Example: Username စစ်တာ username = input("Username: ") if username.isalnum(): print("Username ok") else: print("Special characters မသုံးရ")

.isspace() - Space တွေချည်းလား

စာသားမှာ Space တွေချည်းပဲ ပါလား စစ်တယ်။

isspace_example.py
print(" ".isspace()) # True print(" Hello ".isspace()) # False print("".isspace()) # False (အလွတ်)

.islower() - စာလုံးအသေးတွေလား

စာလုံးတွေက အသေးတွေချည်းလား စစ်တယ်။

islower_example.py
print("hello".islower()) # True print("Hello".islower()) # False print("hello123".islower()) # True (ဂဏန်းက အရေးမပါဘူး)

.isupper() - စာလုံးအကြီးတွေလား

စာလုံးတွေက အကြီးတွေချည်းလား စစ်တယ်။

isupper_example.py
print("HELLO".isupper()) # True print("Hello".isupper()) # False print("HELLO123".isupper()) # True (ဂဏန်းက အရေးမပါဘူး)

5. Modification Methods (ပြင်ဆင်တာ)

.replace() - စာလုံး အစားထိုးတာ

တစ်ခုကို နောက်တစ်ခုနဲ့ အစားထိုးတယ်။

replace_example.py
text = "Hello World" new_text = text.replace("World", "Python") print(new_text) # Output: "Hello Python" # အားလုံး အစားထိုးတယ် text2 = "apple apple apple" new_text2 = text2.replace("apple", "orange") print(new_text2) # Output: "orange orange orange" # Practical Example: phone = "09-123-456-789" clean_phone = phone.replace("-", "") print(clean_phone) # Output: "09123456789"

.split() - ခွဲထုတ်ပြီး List လုပ်တာ

စာသားကို ခွဲပြီး List ပြန်ပေးတယ်။

split_example.py
text = "apple,banana,orange" fruits = text.split(",") print(fruits) # Output: ['apple', 'banana', 'orange'] # Default က Space နဲ့ ခွဲတယ် sentence = "Hello World Python" words = sentence.split() print(words) # Output: ['Hello', 'World', 'Python'] # Practical Example: CSV data ဖတ်တာ csv_line = "John,25,Engineer" data = csv_line.split(",") name, age, job = data print(f"Name: {name}, Age: {age}, Job: {job}")

.join() - List ကို String ပြန်ဖြစ်အောင်

List ထဲက အရာတွေကို ပေါင်းပြီး String တစ်ခု ပြန်လုပ်တယ်။

join_example.py
fruits = ["apple", "banana", "orange"] result = ", ".join(fruits) print(result) # Output: "apple, banana, orange" # Space နဲ့ ဆက်တာ words = ["Hello", "World", "Python"] sentence = " ".join(words) print(sentence) # Output: "Hello World Python" # Practical Example: Path ဆောက်တာ path_parts = ["home", "user", "documents", "file.txt"] file_path = "/".join(path_parts) print(file_path) # Output: "home/user/documents/file.txt"

6. Formatting Methods

.center() - အလယ်မှာ ထားတာ

စာသားကို သတ်မှတ်ထားတဲ့ အကျယ်မှာ အလယ် position ယူတယ်။

center_example.py
text = "Hello" centered = text.center(20) print(f"'{centered}'") # Output: ' Hello ' # Fill character နဲ့ centered2 = text.center(20, "*") print(centered2) # Output: "*******Hello********"

.ljust() - ဘယ်ဖက်မှာ ထားတာ

စာသားကို ဘယ်ဖက်ချိန်ပြီး ညာဖက်မှာ padding ထည့်တယ်။

ljust_example.py
text = "Hello" left_aligned = text.ljust(20) print(f"'{left_aligned}'") # Output: 'Hello ' # Practical Example: Table ဆောက်တာ print("Name".ljust(15) + "Age".ljust(10)) print("Mg Mg".ljust(15) + "25".ljust(10)) print("Kyaw Kyaw".ljust(15) + "30".ljust(10))

.rjust() - ညာဖက်မှာ ထားတာ

စာသားကို ညာဖက်ချိန်ပြီး ဘယ်ဖက်မှာ padding ထည့်တယ်။

rjust_example.py
text = "Hello" right_aligned = text.rjust(20) print(f"'{right_aligned}'") # Output: ' Hello' # Practical Example: ဂဏန်း ချိန်တာ numbers = ["1", "10", "100", "1000"] for num in numbers: print(num.rjust(10))

.zfill() - ရှေ့မှာ သုည ဖြည့်တာ

ဂဏန်း စာသားရဲ့ ရှေ့မှာ သုည (0) တွေ ဖြည့်ပေးတယ်။

zfill_example.py
number = "42" padded = number.zfill(5) print(padded) # Output: "00042" # Practical Example: Invoice number invoice_num = "123" formatted_invoice = invoice_num.zfill(8) print(f"Invoice: {formatted_invoice}") # Output: "Invoice: 00000123"
အကြံပြု: ဒီ String Functions တွေကို အကုန် မှတ်ဖို့ မလိုဘူး။ ဘယ်အချိန်မှာ ဘာသုံးရမယ်ဆိုတာ သိထားရုံပဲ။