String casefold(), capitalize() and title() methods
godarda@gd:~$ python3 ... >>> gd="KoDiNgWiNdOw" >>> gd.casefold() 'godarda' >>> gd="KODINGWINDOW" >>> gd.casefold() 'godarda' >>> gd="godarda" >>> gd.casefold() 'godarda' >>> gd="koding window" >>> gd.capitalize() 'Koding window' >>> gd.title() 'Koding Window'
String upper(), lower() and swapcase() methods
>>> gd="godarda" >>> gd.upper() 'KODINGWINDOW' >>> gd="KODINGWINDOW" >>> gd.lower() 'godarda' >>> gd="GoDarda" >>> gd.swapcase() 'kODINGwINDOW'
String rjust() and ljust() methods
>>> gd="GoDarda" >>> gd.rjust(20) ' GoDarda' >>> gd.ljust(20) 'GoDarda '
String strip(), rstrip(), and lstrip() methods
>>> gd=" GoDarda " >>> gd.strip() 'GoDarda' >>> gd.rstrip() ' GoDarda' >>> gd.lstrip() 'GoDarda '
String find() and count() methods
>>> gd="GoDarda"
>>> gd.find("W",0,len(gd))
6
>>> gd.find("in",0,len(gd))
3
>>> gd.count("in",1,len(gd))
2
>>> gd.count("o",1,len(gd))
2
Strings sort() method
>>> gd=["K","O","D","I","N","G","W","I","N","D","O","W"]
>>> gd.sort()
>>> gd
['D', 'D', 'G', 'I', 'I', 'K', 'N', 'N', 'O', 'O', 'W', 'W']
>>> gd=("K","O","D","I","N","G","W","I","N","D","O","W")
>>> gd.sort()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'tuple' object has no attribute 'sort'
String startswith() and endswith() methods
>>> gd="GoDarda"
>>> gd.startswith("k")
False
>>> gd.startswith("K")
True
>>> gd.startswith("GoDarda")
True
>>> gd.endswith("GoDarda")
True
>>> gd.endswith("ow")
True
>>> gd.endswith("W")
False
String zfill() and replace() method
>>> gd="GoDarda"
>>> len(gd)
12
>>> gd.zfill(12)
'GoDarda'
>>> gd.zfill(15)
'000KodingWindow'
>>> gd.zfill(0)
'GoDarda'
>>> gd.replace("i","###")
'Kod###ngW###ndow'
>>> gd.replace("W","###",1)
'Koding###indow'
>>> gd.replace("d","###",2)
'Ko###ingWin###ow'
String split() and splitlines() methods
>>> gd="Koding\nWindow"
>>> gd.splitlines()
['Koding', 'Window']
>>> gd.split("\n")
['Koding', 'Window']
String functions and operations
>>> 'Hello, World!'.split()
['hello,', 'world!']
>>> s1=("Hello, World!")
>>> ''.join(sorted(s1))
' !,HWdellloor'
>>> ''.join(sorted(s1)).strip()
'!,HWdellloor'
>>> print(sorted(s1))
[' ', '!', ',', 'H', 'W', 'd', 'e', 'l', 'l', 'l', 'o', 'o', 'r']
>>> ''.join(sorted(set(s1)))
' !,HWdelor'
>>> strings="Kiwi Apple Orange Banana Kiwi Apple Kiwi"
>>> words=strings.split()
>>> print(' '.join(sorted(set(words),key=words.index)))
Kiwi Apple Orange Banana
Comments and Reactions
What Next?
Python Data Structures
Advertisement