Python String Methods

အက္ခရာ(alphabet letter)များ a မှ z အထိ ဖြစ်သည်။ နံပါတ်(numbers)များသည် 0 မှ 9 အထိ ဖြစ်သည်။ အက္ခရာ(alphabet letter)များ နှင့် နံပါတ်(numbers)များကို ပေါင်း၍ alphanumeric ဟုခေါ်သည်။ alphanumeric တွင် အက္ခရာ(alphabet letter)များဖြစ်သည့် a မှ z အထိနှင့် နံပါတ်(numbers)များဖြစ်သည့် 0 မှ 9 တို့ပါဝင်သည်။ (space) နှင့် ! # % & ? စသည့် character များသည် alphanumeric တွင် မပါဝင်ပါ။)

အက္ခရာ(alphabet letter)များ၊ နံပါတ်(numbers)များ နှင့် (space) နှင့် ! # % & ? စသည့်တို့ကို character များဟုခေါ်သည်။

dir() function

Python3 တွင် dir() သည် powerful inbuilt function တစ်ခုဖြစ်သည်။ object ၏ attributes များနှင့် method များကို ထုတ်ပေးသည်။ Functions , modules, strings, lists, dictionaries စသည်တို့သည့် object များ ဖြစ်ကြသည်။

dir() function သည် သတ်မှတ်ထားသည့် object များ၏ method များနှင့် properties ကို ပြန်ပေးသည်။ မည်သည့်တန်ဖိုးကို မှ ထုတ်ပေးလိမ့်မည်မဟုတ်ပေ။(dir() function returns all properties and methods of the specified object, without the values)

In [1]:
my_string= "I love Python."
print(dir(my_string))
['__add__', '__class__', '__contains__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'capitalize', 'casefold', 'center', 'count', 'encode', 'endswith', 'expandtabs', 'find', 'format', 'format_map', 'index', 'isalnum', 'isalpha', 'isascii', 'isdecimal', 'isdigit', 'isidentifier', 'islower', 'isnumeric', 'isprintable', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'maketrans', 'partition', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']

1 .capitlize() method

ပထမဆုံး character သည် အက္ခရာ(alphabet letter)ဖြစ်လျှင် ထိုအက္ခရာကို အကြီးစာလုံးသို့ ပြောင်းပေးသည်။(returns a string where the first character is upper case.)

In [2]:
my_string1 = "first letter of the sentence will be capitalized."
print(my_string1.capitalize()) # capitalizes the first letter of the string.  
First letter of the sentence will be capitalized.
In [3]:
my_string1_a = "36 is my age."
print (my_string1_a.capitalize())
36 is my age.

2 .casefold() method

အက္ခရာများအားလုံးကို အသေးစာလုံးသို့ပြောင်းပေးသည်။ (returns a string where all the characters are lower case.) ။.lower()method နှင့်တူညီသော်လည်း.lower() method ထက်ပို အစွမ်းထက်သည်။(casefold() method is stronger, more aggressive)

In [4]:
my_string2 = "THIS SENTENCE WILL BECOME LOWER-CASED"
print(my_string2.casefold()) 
this sentence will become lower-cased
In [5]:
my_string2_a = "THIS SENTENCE WILL BECOME LOWER-CASED"
print(my_string2_a.lower()) 
this sentence will become lower-cased

3 .center() method

Character အရေအတွက်ထည့်ပေးရသည်။ ထို character အရေအတွက်၏ အလယ်တွင် string ကို ဖော်ပြပေးမည်။ (will center align the string, using a specified character (space is default) as the fill character.) အလယ်တွင် string ကို နေရာချထားလိုသည့်အခါတွင် အသုံးပြုသည်။

In [6]:
my_string3 = " I love Python."
print(my_string3.center(60))  
print("----original string below----")
print(my_string3)
                       I love Python.                       
----original string below----
 I love Python.
In [7]:
my_string3 = "banana"
print(my_string3)
x = my_string3.center(20, "O")
print(x)
banana
OOOOOOObananaOOOOOOO
In [8]:
#Character အရေအတွက်ကို ဘယ်လောက်ထည့်ပေးရမယ်ဆိုတာ မစဥ◌စားဘဲ 
#အရှေ့ (၂)လုံး၊အရှေ့ (၂)လုံး ပေါင်း (၄)လုံးထည့်မယ်ဆိုပြီးလည်း ရေးလို့ရပါတယ်။ 
print(my_string3.center(len(my_string3)+4, "@"))
@@banana@@

4 .count() method

စာကြောင်း သို့မဟုတ် စာလုံး(word)အတွင်း၌ မိမိသတ်မှတ်ပေးလိုက်သည့် အက္ခရာ၏ အရေအတွက်ကို ဖော်ပြပေးသည်။ (returns the number of elements with the specified value.)။ String အတွက်သာမက number, list, tuple စသည့်အမျိုးအစားများအတွင်းရှိ မိမိသတ်မှတ်ပေးလိုက်သည့် အက္ခရာ၏ အရေအတွက်ကိုလည်း ဖော်ပြပေးနိုင်သည်။

In [9]:
my_string4 = "fdfposGJsufH23edHHHHye23edffosidgs23edHHae66623ed6hw23edgw"
print(my_string4.count("H")) # returns the count of string "23" in the original string.
7

မိမိသတ်မှတ်ပေးသည့် starting index နေရာမှ စ၍ ending index အဆုံးနေရာအတွင်း၌သာ ရေတွက်ခြင်း(counting) ပြုလုပ်နိုင်သည်။ ("string being serached", starting index (optional), ending index(optional))

In [10]:
my_string4 = "fdfposGJsufH23edHHHHye23edffosidgs23eduiae66623ed6hw23edgw"
print(my_string4.count("H", 0, 20)) 
5

5 .encode() method

ထည့်ပေးသည့် string ကို သတ်မှတ်လိုက်သည့် encoding မျိုးဖြင့် encode လုပ်ပေးသည်။ ဘာမှ မသတ်မှတ်ပေးလျှင် UTF-8 ဖြင့် encode လုပ်ပေးသည်။(encodes the string, using the specified encoding. If no encoding is specified, UTF-8 will be used.)

In [11]:
my_string5 = "My name is Ståle"
x=my_string5.encode(encoding="utf-8", errors="strict")
print(x)
print("----original string below----")
print(my_string5)
b'My name is St\xc3\xa5le'
----original string below----
My name is Ståle
In [12]:
print(my_string5.encode(encoding="ascii",errors="backslashreplace"))
b'My name is St\\xe5le'

6 .endswith() method

အဆုံး အက္ခရာများကို စစ်ပေးသည်။ သတ်မှတ်ထားသည့် character နှင့် အဆုံးသတ်သည် ဆိုလျှင် True ပြန်ပေးသည်။ ထိုသို့မဟုတ်လျှင် False ပေးသည်။ (returns True if the string ends with the specified value, otherwise False.)
string.endswith(value, start, end)အစနှင့် အဆုံးထည့်ပေးလျှင် ထိုအစနှင့် အဆုံးအတွင်း၌သာ ရှာသည်။

In [13]:
my_string6 = "I love Python."
print(my_string6.endswith("hon."))
# Returns True if the original string ends in the string passed as argument. Otherwise returns False  
print("----original string below----")
print(my_string6)
True
----original string below----
I love Python.

7 .expandtabs() method

ပုံမှန်အားဖြင့် \t သည် tab တစ်ခုစာ ခြားပေးသည်။ Tab တစ်ခုသည် space (၄)နေရာ(4 spaces)နှင့် ညီသည်။
.expandtabs() method ကိုသုံး၍ မိမိအလိုရှိသည့် space အရေအတွက် အတိုင်းခြားပေးသည်။ ဥပမာ .expandtabs(10) သည် space (၁၀) နေရာ(10 spaces) ခြားပေးသည်။ string.expandtabs(tabsize)

In [14]:
my_string7 = "I love \tPython."
print(my_string7.expandtabs(0))  # it expands the tab character from original 4 spaces to the number passed as argument.
print("----original string below----")
print(my_string7)
I love Python.
----original string below----
I love 	Python.
In [15]:
txt = "H\te\tl\tl\to"
x =  txt.expandtabs(0)# character တစ်လုံးနှင့် တစ်လုံးအကြားတွင် space နေရာ လုံးဝမထားရန်
x3 =  txt.expandtabs(3)# character တစ်လုံးနှင့် တစ်လုံးအကြားတွင် space (၃)နေရာ ခြားပေးရန်
print(x)
print(x3)
Hello
H  e  l  l  o

8 .find() method

.find() method သည် ရှာခိုင်းသည့် အက္ခရာ ၊ စာလုံး၏ ပထမဆုံး တွေ့ရှိသည့်တည်ရှိရာနေရာကို ဖော်ပြပေးသည်။ (finds the first occurrence of the specified value.)။ ထိုအက္ခရာ ၊ ထိုစာလုံး မရှိပါက -1 ထုတ်ပေးသည်။ (returns -1 if the value is not found.)

In [16]:
my_string8 = "I love Python."
print(my_string8.find("Py"))  
print(my_string8.find("My")) 
# tells starting index of the string that is passed as argument inside .find() method.
7
-1

9 .format() method

.format() method ကို သုံး၍ string ကို မိမိအလိုရှိသလို ပုံစံချ(format) နိုင်သည်။(returns the formatted string.)
တွန့်ကွင်း(curly brackets: {}) သည် placeholder ဖြစ်သည်။ string ၏ placeholder အတွင်း၌ မိမိအလိုရှိသည့် specified value(s) ကို ထည့်နိုင်သည်။ output များကို ဖော်ပြရာတွင် အလွန်အသုံးဝင်သည်။

In [17]:
my_string9 = " I love {} Python {}.  "
print(my_string9.format("placeholder1", "placeholder2")) 
# places the arguments positionally in the placeholders inside string.
print("----original string below----")
print(my_string9)
 I love placeholder1 Python placeholder2.  
----original string below----
 I love {} Python {}.  
In [18]:
my_string9_a = "For only {price:.2f} dollars!" # Example
print(my_string9_a.format(price = 49))
For only 49.00 dollars!
In [19]:
myorder = "I have a {carname}, it is a {model}." # Example
print(myorder.format(carname = "Ford", model = "Mustang"))
I have a Ford, it is a Mustang.
In [20]:
#Example
quantity = 3
itemno = 567
price = 49
myorder = "I want {} pieces of item number {} for {:.2f} dollars."
print(myorder.format(quantity, itemno, price))
I want 3 pieces of item number 567 for 49.00 dollars.

Index number များ ကို အသုံးပြု၍လည်း လုပ်နိုင်သည်။ ထို index number ကို curly brackets { } အတွင်း ထည့်ပေးရမည်။ (can use index numbers (a number inside the curly brackets {0}) to be sure the values are placed in the correct placeholders)။
အောက်တွင် ဥပမာ ဖြင့် ဖော်ပြထားသည်။

In [21]:
#Example  (refer to the same value more than once, use the index number:)
age = 36
name = "John"
txt = "His name is {1}. {1} is {0} years old."
print(txt.format(age, name))
His name is John. John is 36 years old.

10 .format_map() method

.format_map() method ကို dictionary များ၏ key နှင့် value ဖော်ပြရာတွင် အသုံးပြုသည်။ (used to return an dictionary key's value.)။ Dictionary တွင် အတွဲလိုက်ရှိသည့် key နှင့် value ကို အတူတကွ ဖော်ပြရန်အတွက် သုံးသည်။

In [22]:
my_dict = { "name" : "Kaung",
             "birth city": "Yangon" } 
# dictionary that will be passed as argument to .format_map() method.

my_string10 = " {name}'s place of birth is {birth city}  "
print(my_string10.format_map(my_dict))
# Takes a single argument which is input dictionary. Places the values in place of keys mentioned.
 Kaung's place of birth is Yangon  

11 .index() method

ပထမဆုံးတွေ့သည့် နေရာကို ဖော်ပြပေးသည်။(returns the position at the first occurrence of the specified value.)။
"I love Python." တွင် o (၂)လုံးပါရှိရာ မည့်သည့်နေရာအတွင်းရှာပေးပါဟု မပြောလျှင် အစမှ အဆုံးအတွင် ရှာပေးပြီး ပထမဆုံးတွေ့သည့်နေရာကို ဖော်ပြပေးသည်။ ဒုတိယ o အတွက် အစ နှင့် အဆုံးနေရာ သတ်မှတ်ပေးသည်။

In [23]:
my_string11 = "I love Python." 
print(my_string11.index("o"))   # first occurrence
print(my_string11.index("o", 5, 12))
3
11

12 .isalnum()method

Character များအားလုံးသည် alphanumeric ဖြစ်နေလျှင် True ပြန်ပေးသည်။ Alphanumeric တွင် အက္ခရာ(alphabet letter)များဖြစ်သည့် a မှ z အထိနှင့် နံပါတ်(numbers)များဖြစ်သည့် 0 မှ 9 အတွင်း တို့ပါဝင်သည်။ (space) နှင့် ! # % & ? စသည့် character များသည် alphanumeric တွင် မပါဝင်ပါ။)

In [24]:
my_string12 = "LovePython3"
print(my_string12)
print(my_string12.isalnum())      
# checks whether all the characters in a given string are alphanumeric or not. Returns boolean
LovePython3
True
In [25]:
my_string12_a = "I love Python"
print(my_string12_a)
print(my_string12_a.isalnum()) 
# alphanumeric means only alphabet or numbers. So this returns false as it includes spaces.
I love Python
False

13 .isalpha() method

Character များအားလုံးသည် a မှ z အထိ အက္ခရာ(alphabet letter)ထဲမှ ဖြစ်နေလျှင် True ပြန်ပေးသည်။ (returns True if all the characters are alphabet letters (a-z))။
အက္ခရာ(alphabet letter) မဟုတ်သည့် character များမှာ 0 မှ 9 အထိ နံပါတ်(numbers)များနှင့် (space) နှင့် ! # % & ? စသည့်တို့ ဖြစ်သည်။

In [26]:
my_string13 = "Kaung" #alphabet
print(my_string13)
print(my_string13.isalpha())  
# checks whether the string consists of alphabetic characters only or not.
Kaung
True
In [27]:
my_string13_a = "I love Python" # not alphabet because of "space"
print(my_string13_a)
print(my_string13_a.isalpha())
I love Python
False

14 .isascii() method

ASCII characters ဆိုသည်မှာ print ထုတ်၍ ရသည့် characters ( that have a printable representation)ဖြစ်သည်။ U+0000 မှ U+007F အတွင်း ဖြစ်သည်။
For example, ° is changed to \xb0.

In [28]:
my_string14 = "I love Python."
print(my_string14)
print(my_string14.isascii())  # checks whether all characters in the string are ASCII or not.
I love Python.
True
In [29]:
my_string14_a = "ÆðþŊ"   # non-ASCII characters 
print(my_string14_a)
print(my_string14_a.isascii())
ÆðþŊ
False
In [30]:
print(ascii('°'))
print(type(ascii('°')))
'\xb0'
<class 'str'>

15 .isdecimal() method

Character များ အားလုံးသည် ဒဿမကိန်းများ ဖြစ်နေလျှင် True ပြန်ပေးသည်။ (returns True if all the characters are decimals (0-9).)။ ထိုသို့မဟုတ်လျှင် False ပြန်ပေးသည်။

In [31]:
my_string15 = "64329"
print(my_string15)
print(my_string15.isdecimal())
64329
True
In [32]:
my_string15_a = "64f3 29"
print(my_string15_a)
print(my_string15_a.isdecimal())
64f3 29
False
In [33]:
my_string15_b = "\u0033" #unicode for 3
print(my_string15_b.isdecimal())
True
In [34]:
my_string15_c = "\u0030" #unicode for 0
print(my_string15_c.isdecimal())

my_string15_d = "\u0047" #unicode for G
print(my_string15_d.isdecimal())
True
False

16 .isdigit() method

Character များ အားလုံးသည် digit များ ဖြစ်နေလျှင် True ပြန်ပေးသည်။ ထိုသို့မဟုတ်လျှင် False ပြန်ပေးသည်။ (returns True if all the characters are digits, otherwise False.)

In [35]:
my_string16 = "6574292"
print(my_string16)
print(my_string16.isdigit())     
6574292
True
In [36]:
my_string16_a = " 837hghhh4834  "    
print(my_string16_a)
print(my_string16_a.isdigit())   
# this returns false becuase it includes spaces and alphabet.
 837hghhh4834  
False
In [37]:
my_string16_b = "\u0030" #unicode for 0
print(my_string16_b.isdigit())

my_string16_c  = "\u00B2" #unicode for ²
print(my_string16_c.isdigit())
True
True

17 .isidentifier() method

String သည် valid identifier တစ်ခု ဖြစ်လျှင် True ပြန်ပေးသည်။ ထို့သို့ မဖြစ်ခဲ့ လျှင် False ပြန်ပေးသည်။ (returns True if the string is a valid identifier, otherwise False.)
String တစ်ခုသည် သည် alphanumeric letters (a-z) and (0-9),ဖြစ်နေလျှင်သော်လည်းကောင်း၊ underscores (_) ပါဝင်နေလျှင်သော်လည်းကောင်းvalid identifier ဖြစ်သည်။ valid identifier တစ်ခု ဖြစ်ရန် number ဖြင့်မစရ သို့မဟုတ် spaces မပါဝင်။

In [38]:
my_string17 = "I love Python."
print(my_string17.isidentifier())     
False
In [39]:
a = "MyFolder"
print(a.isidentifier())

b = "Demo002"
print(b.isidentifier())

c = "2bring"
print(c.isidentifier())

d = "my demo"
print(d.isidentifier())
True
True
False
False

18 .islower() method

Character များ အားလုံးသည် အသေးစာလုံး(lower case)များ ဖြစ်နေလျှင် True ပြန်ပေးသည်။ ထိုသို့မဟုတ်လျှင် False ပြန်ပေးသည်။ (returns True if all the characters are in lower case, otherwise False.) နံပါတ်များ(Numbers) ၊ သင်္ကေတများ(symbols )နှင့် space များကို မစစ်ဆေးပါ။ alphabet များကို သာစစ်ဆေးပါသည်။

In [40]:
my_string18 = "I love Python."
print(my_string18.islower())   
False
In [41]:
a = "Hello world!"
print(a.islower())

b = "hello 123"
print(b.islower())

c = "mynameisPeter"
print(c.islower())
False
True
False

19 .isnumeric() method

Character များ အားလုံးသည် နံပါတ်များ(numeric (0-9)) ဖြစ်နေလျှင် True ပြန်ပေးသည်။ ထိုသို့မဟုတ်လျှင် False ပြန်ပေးသည်။(returns True if all the characters are numeric (0-9), otherwise False.)
ထပ်ကိန်းများ နှင့် အပိုင်းကိန်းများကိုလည်း နံပါတ်များ(numeric) အဖြစ် သတ်မှတ်သည်။(Exponents, like ² and ¾ are also considered to be numeric values.)

In [42]:
my_string19 = "I love Python."
print(my_string19.isnumeric())        # checks whether the string is numeric or not.  
False
In [43]:
a = "\u0030" #unicode for 0
print(a.isnumeric())

b = "\u00B2" #unicode for &sup2;
print(b.isnumeric())

c = "10km2"
print(c.isnumeric())
True
True
False

20 .isprintable() method

Character များ အားလုံးသည် ပုံနှိပ်ဖော်ပြနိုင်သည့် စာလုံးများ ဖြစ်နေလျှင် True ပြန်ပေးသည်။ ထိုသို့မဟုတ်လျှင် False ပြန်ပေးသည်။ (returns True if all the characters are printable, otherwise False.)
ပုံနှိပ်ဖော်ပြနိုင်သည့် စာလုံးများ ဟုတ် မဟုတ် စစ်ဆေးရန် သုံးသည်။

In [44]:
my_string20 = "I love Python."
print(my_string20.isprintable())# checks whether the string can be printed or not. 
True
In [45]:
my_string20_a = "Hello!\nAre you #1?"
x = my_string20_a .isprintable()
print(x)
False

21 .isspace() method

String တစ်ခုအတွင်းရှိ character များ အားလုံးသည် whitespaces ဖြစ်နေလျှင် True ပြန်ပေးသည်။ ထိုသို့မဟုတ်လျှင် False ပြန်ပေးသည်။(returns True if all the characters in a string are whitespaces, otherwise False.)

In [46]:
my_string21 = "I love Python."
print(my_string21.isspace()) # checks whether the string includes only spaces or not.
False
In [47]:
my_string2_a = "     "
print(my_string2_a.isspace())

my_string2_b = "   K  "
print(my_string2_b.isspace())
True
False

22 .istitle() method

စာလုံးများအားလုံးသည် အကြီးစာလုံး(upper case letter)ဖြင့် စပြီး ကျန်စာလုံးများသည် အသေးစာလုံး(lower case letters) ဖြစ်လျှင် True ပြန်ပေးသည်။ ထိုသို့မဟုတ်လျှင် False ပြန်ပေးသည်။ (returns True if all words in a text start with a upper case letter, AND the rest of the word are lower case letters, otherwise False.)

သင်္ကေတများ(symbols) နှင့် နံပါတ်များ(numbers)နှင့်မသက်ဆိုင်ပါ။(Symbols and numbers are ignored.)

In [48]:
my_string22 = " I love Python.  "
print(my_string22)
print(my_string22.istitle()) # checks whether the string is a title or not. For a string to be a title, its every word should start with a capital letter. 
 I love Python.  
False
In [49]:
my_string22_a = " I love Python.  "
print(my_string22_a.istitle()) 
False
In [50]:
a = "HELLO, AND WELCOME TO MY WORLD"
print(a.istitle())

b = "Hello"
print(b.istitle())

c = "22 Names"
print(c.istitle())

d = "This Is %'!?"
print(d.istitle())
False
True
True
True

23 .isupper() method

Character များ အားလုံးသည် အကြီးစာလုံး(upper case)ဖြစ်နေလျှင် True ပြန်ပေးသည်။ ထိုသို့မဟုတ်လျှင် False ပြန်ပေးသည်။ (returns True if all the characters are in upper case, otherwise False.)
သင်္ကေတများ(Symbols) နှင့် နံပါတ်များ(numbers)နှင့် မသက်ဆိုင်ပါ။ အက္ခရာများ(alphabet characters)ကိုသာ စစ်ဆေးသည်။ (Numbers, symbols and spaces are not checked, only alphabet characters.)

In [51]:
my_string23 = "KAUNG HTAT NYUNT"
print(my_string23.isupper())         # checks whether the string is all upper-case or not.
True
In [52]:
my_string23_a = "Kaung Htat Nyunt"
print(my_string23_a.isupper()) 
False
In [53]:
a = "@WORLD"
print(a.isupper())

b = "HELLO 123"
print(b.isupper())
True
True

24 .join() method

String မှ character တစ်လုံးချင်းစီ( iterable items)ကို ယူ၍ သတ်မှတ်ထားသည့် character နှင့် ပေါင်းပေးသည်။(takes all items in an iterable and joins them into one string.)

In [54]:
my_string24 = " IlovePython"
print("--".join(my_string24))  
print("----original string below----")
print(my_string24)
 --I--l--o--v--e--P--y--t--h--o--n
----original string below----
 IlovePython
In [55]:
my_string24 = " IlovePython"
print(" ".join(my_string24))   # it takes any iterable as an argument. In this case I am passing string. 
                                # it is joined using -- here in my example.

print("----original string below----")
print(my_string24)
  I l o v e P y t h o n
----original string below----
 IlovePython

25 .ljust() method

String ကို ဘယ်ညှိပေးသည်။ (will left align the string, using a specified character (space is default) as the fill character.)။ သတ်မှတ်ထားသည့် character အရေအတွက် ထည့်ပေးရသည်။

In [56]:
my_string25 = "I love Python."
print(my_string25.ljust(70,'*'))   # Here the space mentioned is 70. 
print("----original string below----")
print(my_string25)
I love Python.********************************************************
----original string below----
I love Python.
In [57]:
txt = "banana"
x = txt.ljust(20, "_")
print(x)
banana______________

26 .lower() method

String တစ်ခုမှ character များကို အသေးစာလုံး()အဖြစ်သိုပြောင်းပေးသည်။ (converts all uppercase characters to lowercase.) String တွင် အကြီးစာလုံး(uppercase character)များ မပါရှိပါက မူလ string အတိုင်းသာ ဖြစ်လိမ့်မည်။

In [58]:
my_string26 = " I love Python.  "
print(my_string26.lower())  # converts each character in the string to lower-case. 
print("----original string below----")
print(my_string26)
 i love python.  
----original string below----
 I love Python.  

27 .lstrip() method

Character ၏ အရှေ့တွင် ရှိသည့် မလိုလားအပ်သည့် space, comma စသည်တို့ကို ဖယ်ရှားပေးသည်။(removes any leading characters (space is the default leading character to remove))

In [59]:
my_string27 = "          I love Python.  "
print(my_string27.lstrip())   # it strips all the spaces present at the left side of the string. 
print("----original string below----")
print(my_string27)
I love Python.  
----original string below----
          I love Python.  
In [60]:
txt = ",,,,,ssaaww.....banana"
x = txt.lstrip(",.asw")
print(x)
banana

28 .maketrans() method + translate() method

.maketrans() method သည် translate() function တွင် သုံးရန်အတွက် translate table ကို ထုတ်ပေးသည်။ (This method returns a translate table to be used translate() function.)။ Translation လုပ်ပြီး dictionary ထဲတွင် key, value pair အဖြစ်သိမ်းဆည်းထားသည်။

In [61]:
my_string28 = "I love Python."
translation = my_string28.maketrans("IFHn", "1234")  
# it creates a translation and store them as key, value pair in dictionary.
print(translation)                                   
# translation is stored as Unicode representation of each character.

print(chr(73))
print(my_string28.translate(translation))  
# translate() method applies the translation. 
# It means it replaces each character with its corresponding translation that we generated earlier

print("----original string below----")
print(my_string28)
{73: 49, 70: 50, 72: 51, 110: 52}
I
1 love Pytho4.
----original string below----
I love Python.

29 .partition() method

.partition() method သည် သတ်မှတ်ပေးသည့်(specified) string ကို ရှာဖွေပြီး string ကို tuple (၃)ခု ဖြစ်အောင်ခွဲသည်။
သတ်မှတ်ပေးသည့်(specified string)ကို နောက်ဆုံးတွေ့သည့်နေရာကို မူတည်၍ tuple (၃)ခု ဖြစ်အောင်ခွဲသည်။ သတ်မှတ်ပေးသည့်(specified string) မတိုင်ခင်နေရာ(everything before the "match") သတ်မှတ်ပေးသည့်(specified string) ပါသည့်နေရာ( element contains the specified string.) သတ်မှတ်ပေးသည့်(specified string) ပါသည့်နေရာ နောက်ပိုင်း(everything after the "match") string.partition(value)

In [62]:
my_string29 = " I love Python."
print(my_string29.partition("love"))   # it breaks the string in three parts. The argument passed is the separator. It is not to be confused with split. It always returns tuple having 3 items, keeping the string passed as argument as the middle item.
print("----original string below----")
print(my_string29)
(' I ', 'love', ' Python.')
----original string below----
 I love Python.
In [63]:
my_string29_a = "I love apple."
print(my_string29_a.partition("love"))   
# it breaks the string in three parts. 
#The argument passed is the separator. 
#It is not to be confused with split. 
#It always returns tuple having 3 items, keeping the string passed as argument as the middle item.
('I ', 'love', ' apple.')

30 .replace() method

စာလုံး(phrase)တစ်ခုကို တခြားစာလုံး(phrase) ဖြင့် အစားထိုးသည်။ (replaces a specified phrase with another specified phrase.) အရှေ့စာလုံးသည် အစားထိုးခံရမည့်စာလုံး(phrase)ဖြစ်သည်။ အနောက်စာလုံးသည် အစားထိုးမည့် စာလုံး(phrase)ဖြစ်သည်။

In [64]:
my_string30 = " I love Python.  "
print(my_string30.replace("Python", "apple"))  
# it raplaces the string passed as first arguments with the string passed as second argument.
print("----original string below----")
print(my_string30)
 I love apple.  
----original string below----
 I love Python.  

31 .rfind()method

နောက်ဆုံးတွေ့ခဲ့သည့်နေရာကို ဖော်ပြပေးသည်။(finds the last occurrence of the specified value.)။ အတူညီဆုံးဖြစ်သည့်နေရာကို ရှာပေးသည်။ (returns the starting index of the right most substring that matches)
တူသည့်စာကို ရှာမတွေ့ပါက -1 ထုတ်ပေးသည်။ (returns -1 if the value is not found.)
rfind() method သည် rindex() method ခပ်ဆင်ဆင်နှင့်တူညီသည်။

In [65]:
my_string31 = "I love Python."
print(my_string31.rfind("Py")) 

my_string31 = "I love Python and Python."
print(my_string31.rfind("Py"))
7
18

32 .rindex() method

နောက်ဆုံးတွေ့ခဲ့သည့်နေရာကို ဖော်ပြပေးသည်။(finds the last occurrence of the specified value.)။ အတူညီဆုံးဖြစ်သည့်နေရာကို ရှာပေးသည်။ (returns the starting index of the right most substring that matches) တူသည့်စာကို ရှာမတွေ့ပါက -1 ထုတ်ပေးသည်။ (returns -1 if the value is not found.) rindex() method သည် rfind() method ခပ်ဆင်ဆင်နှင့်တူညီသည်။

In [66]:
my_string32 = " I love Python."
print(my_string32.rindex("Py"))   # it returns the index of the rightmost substring that matches the string passed as argument. 
print("----original string below----")
print(my_string32)
8
----original string below----
 I love Python.
In [67]:
txt = "Hello, welcome to my world."
x = txt.rindex("o")
print(x)
22

33 .rjust() method

စာလုံးရေ ထည့်ပေးရသည်။ ထိုစာလုံးရေ နေရာတွင် ညာညှိ(right justified)ပေးသည်။(will right align the string, using a specified character (space is default) as the fill character.)

In [68]:
my_string33 = " I love Python.  "
print(my_string33.rjust(50, "*"))  
# စာလုံးရေ (၅၀)စာနေရာ(50 characters long)တွင် ညာညှိ(right justified)ပေးသည်။
print("----original string below----")
print(my_string33)
********************************* I love Python.  
----original string below----
 I love Python.  

34 .rpartition() method

.rpartition() method သည် သတ်မှတ်ပေးသည့်(specified) string ကို ရှာဖွေပြီး string ကို tuple (၃)ခု ဖြစ်အောင်ခွဲသည်။
သတ်မှတ်ပေးသည့်(specified string)ကို နောက်ဆုံးတွေ့သည့်နေရာကို မူတည်၍ tuple ၃ခု ဖြစ်အောင်ခွဲသည်။ သတ်မှတ်ပေးသည့်(specified string) မတိုင်ခင်နေရာ(everything before the "match") သတ်မှတ်ပေးသည့်(specified string) ပါသည့်နေရာ( element contains the specified string.) သတ်မှတ်ပေးသည့်(specified string) ပါသည့်နေရာ နောက်ပိုင်း(everything after the "match") string.rpartition(value)

In [69]:
my_string34 = "I am a student learning Computer Science. Computer Science is an amazing discipline. "
print(my_string34.rpartition("Computer"))  
print("----original string below----")
print(my_string34)
('I am a student learning Computer Science. ', 'Computer', ' Science is an amazing discipline. ')
----original string below----
I am a student learning Computer Science. Computer Science is an amazing discipline. 

35 .rsplit() method

String တစ်ခုကို list တစ်ခု အဖြစ်ခွဲပေးသည်။ ညာဘက်စ၍ ခွဲပေးသည်။ (splits a string into a list, starting from the right.) "max" ကို မသတ်မှတ်ပေးလျှင် split() method မှ ထုတ်ပေးသည့် ရလဒ်နှင့် တူညီလိမ့်မည်။
Note: When maxsplit is specified, the list will contain the specified number of elements plus one.

In [70]:
my_string35 = "I love Python."
print(my_string35.rsplit(" "))     

my_string35_a= "apple, banana, cherry"
print(my_string35_a.rsplit(",", 2))
['I', 'love', 'Python.']
['apple', ' banana', ' cherry']

36 .rstrip() method

String တစ်ခုရှိ အဆုံး (trailing) character များကို ဖယ်ရှားပေးသည်။ string ၏ နောက်ဆုံး၌ ရှိသည့် space သည် ဖယ်ရှားပေ ရမည့်အရာဖြစ်သည်။ (removes any trailing characters (characters at the end a string), space is the default trailing character to remove.)

In [71]:
my_string36 = " I love Python....."
print(my_string36.rstrip("."))                    
# it strips off all the spaces present at the right side of the string.
 I love Python
In [72]:
my_string36_a = "banana,,,,,ssaaww....."
print(my_string36_a.rstrip(",.asw"))
banan

37 .split() method

String တစ်ခုကို list တစ်ခု အဖြစ်ခွဲပေးသည်။ (splits a string into a list.) separator ကို သတ်မှတ်ပေးနိုင်သည်။ (can specify the separator, default separator is any whitespace.)
string.split(separator, maxsplit)

In [73]:
my_string37 = "I love Python."
print(my_string37.split()) 
print(my_string37.split(" ")) # same result
['I', 'love', 'Python.']
['I', 'love', 'Python.']

38 .splitlines() method

String တစ်ခုကို list တစ်ခု အဖြစ်ခွဲပေးသည်။(splits a string into a list. ) စာ‌ကြောင်းအဆုံးတွင် line break ဖြစ်သည်။ (The splitting is done at line breaks.) string.startswith(value, start, end)

In [74]:
my_string38 = " I love Python.\n I plan to learn Python because it is fun.\n These lines will be separated. "
print(my_string38)
print("---------------------------------------  ")
print(my_string38.splitlines())  
print(my_string38.splitlines(True))  
 I love Python.
 I plan to learn Python because it is fun.
 These lines will be separated. 
---------------------------------------  
[' I love Python.', ' I plan to learn Python because it is fun.', ' These lines will be separated. ']
[' I love Python.\n', ' I plan to learn Python because it is fun.\n', ' These lines will be separated. ']

39 .startswith() method

String တစ်ခုသည် သတ်မှတ်ထားသည့် character ဖြင့် စထားလျှင် True ပြန်ပေးသည်။ ထိုသို့မဟုတ်လျှင် False ပြန်ပေးသည်။( returns true if the string begins with the characters, and false if not.) အကြီးစာလုံး အသေးစာလုံးမှန်ရန် လိုအပ်သည်။ case sensitive ဖြစ်သည်။) string.startswith(value, start, end)

In [75]:
my_string39 = "I love Python.  "
print(my_string39.startswith("i")) 
print("----original string below----")
print(my_string39)
False
----original string below----
I love Python.  
In [76]:
my_string39_a = "Hello, welcome to my world."
print(my_string39_a.startswith("wel", 7, 20))
True

40 .strip() method

String ၏ အစနှင့် အဆုံးနေရာတို့တွင် ရှိနေသည့် space များကို ဖယ်ရှားပေးသည်။ (Remove spaces at the beginning and at the end of the string)

In [77]:
my_string40 = "         I love Python.         "
print(my_string40.strip())           #it strips off all the spaces present at the right side of the string.
print("----original string below----")
print(my_string40)
I love Python.
----original string below----
         I love Python.         

41 .swapcase() method

String တစ်ခုရှိ အကြီးစာလုံးများ(upper case letters)ကို အသေးစားလုံး(lower case )များ အဖြစ်သို့လည်းကောင်း အသေးစားလုံး(lower case )များကို အကြီးစာလုံးများ (upper case letters)ဖြစ်သို့လည်းကောင် ပြောင်းပေးသည်။ (returns a string where all the upper case letters are lower case and vice versa.) string.swapcase()

In [78]:
my_string41 = "I love Python."
print(my_string41.swapcase())   
i LOVE pYTHON.

42 .title() method

String တစ်ခုကို ခေါင်းစဥ(title) အဖြစ်ပြောင်းပေးသည်။ ခေါင်းစဥ(title)ဆိုသည်မှာ စာလုံး၏ အစ အက္ခရာသည် အကြီးစာလုံး ဖြစ်ရမည်။ စာလုံး(word)တိုင်း၏ ပထမ character ကို အကြီးစာလုံး(upper case)အဖြစ်ပြောင်းပေးသည်။ (returns a string where the first character in every word is upper case. Like a header, or a title.)
စာလုံး(word)တွင် သင်္ကေတများ( symbols )နှင့် နံပါတ်များ (numbers)ပါဝင်နေပါက ထို symbol သို့မဟုတ် နံပါတ်(number) နောက် character ကို ပြောင်းပေးသည်။ (If the word contains a number or a symbol, the first letter after that will be converted to upper case.)

In [79]:
my_string42 = " I love Python.  "
print(my_string42.title())     # it makes first alphabet of each word capital. 

my_string42_a = "Welcome to my 2nd world"
print(my_string42_a .title())
 I Love Python.  
Welcome To My 2Nd World

43 .upper() method

String တစ်ခု အတွင်းရှိ character များအားလုံးကို အကြီးစာလုံး(upper case) ဖြစ်အောင် ပြောင်းပေးသည်။ (returns a string where all characters are in upper case.) သင်္ကေတများ( symbols )နှင့် နံပါတ်(numbers)များ မသက်ဆိုင်ပါ။ (Symbols and numbers are ignored.)

In [80]:
my_string44 = " I love Python.  "
print(my_string44.upper())   # it makes all the characters capital.
 I LOVE PYTHON.  
In [81]:
my_string44_a = "this is string example....wow!!!"
print (my_string44_a.upper())
THIS IS STRING EXAMPLE....WOW!!!

44 .zfill() method

String ၏ အစတွင် zeros (0) ထည့်ပေးသည်။ မိမိသတ်မှတ်ထားသည့် အရှည်(specified length) ပြည့်အောင် ထည့်ပေးသည်။ (adds zeros (0) at the beginning of the string, until it reaches the specified length.)
.len() method ကို သုံး၍ မိမိအလိုရှိသည့် zeros (0) အရေအတွက်အတိုင်း ထည့်ပေါင်းပေးနိုင်သည်။ String ၏ အရှည်(length) မည်မျှ ရှိသည်ကို ရေတွက်နေရန် မလိုတော့ပေ။
string ၏ အရှည်သည် မိမိသတ်မှတ်ထားသည့် အရှည်(specified length)ထက်ပိုများနေပါက မထည့်ပေးပါ။ (If the value of the len parameter is less than the length of the string, no filling is done.)

string.zfill(len)

In [82]:
my_string45 = "I love Python."
print(my_string45.zfill(30))     
print(my_string45.zfill(len(my_string45)+3)) 
print(my_string45.zfill(10))
0000000000000000I love Python.
000I love Python.
I love Python.
In [83]:
a = "hello"
print(a.zfill(10))

b = "welcome to the jungle"
print(b.zfill(10))

c = "10.000"
print(c.zfill(10))
00000hello
welcome to the jungle
000010.000

-End-