String casefold(), capitalize() and title() methods
kodingwindow@kw:~$ python3 ... >>> kw="KoDiNgWiNdOw" >>> kw.casefold() 'kodingwindow' >>> kw="KODINGWINDOW" >>> kw.casefold() 'kodingwindow' >>> kw="kodingwindow" >>> kw.casefold() 'kodingwindow' >>> kw="koding window" >>> kw.capitalize() 'Koding window' >>> kw.title() 'Koding Window'
String upper(), lower() and swapcase() methods
>>> kw="kodingwindow" >>> kw.upper() 'KODINGWINDOW' >>> kw="KODINGWINDOW" >>> kw.lower() 'kodingwindow' >>> kw="Kodingwindow" >>> kw.swapcase() 'kODINGWINDOW'
String rjust() and ljust() methods
>>> kw="Kodingwindow" >>> kw.rjust(20) ' Kodingwindow' >>> kw.ljust(20) 'Kodingwindow '
String strip(), rstrip(), and lstrip() methods
>>> kw=" Kodingwindow " >>> kw.strip() 'Kodingwindow' >>> kw.rstrip() ' Kodingwindow' >>> kw.lstrip() 'Kodingwindow '
String find() and count() methods
>>> kw="Kodingwindow" >>> kw.find("w",0,len(kw)) 6 >>> kw.find("in",0,len(kw)) 3 >>> kw.count("in",1,len(kw)) 2 >>> kw.count("o",1,len(kw)) 2
Strings sort() method
>>> kw=["K","O","D","I","N","G","W","I","N","D","O","W"] >>> kw.sort() >>> kw ['D', 'D', 'G', 'I', 'I', 'K', 'N', 'N', 'O', 'O', 'W', 'W'] >>> kw=("K","O","D","I","N","G","W","I","N","D","O","W") >>> kw.sort()Traceback (most recent call last): File "<stdin>", line 1, in <module> AttributeError: 'tuple' object has no attribute 'sort'
String startswith() and endswith() methods
>>> kw="Kodingwindow" >>> kw.startswith("k") False >>> kw.startswith("K") True >>> kw.startswith("Kodingwindow") True >>> kw.endswith("Kodingwindow") True >>> kw.endswith("ow") True >>> kw.endswith("W") False
String zfill() and replace() method
>>> kw="Kodingwindow" >>> len(kw) 12 >>> kw.zfill(12) 'Kodingwindow' >>> kw.zfill(15) '000Kodingwindow' >>> kw.zfill(0) 'Kodingwindow' >>> kw.replace("i","###") 'Kod###ngw###ndow' >>> kw.replace("w","###",1) 'Koding###indow' >>> kw.replace("w","###",2) 'Koding###indo###'
String split() and splitlines() methods
>>> kw="Koding\nwindow" >>> kw.splitlines() ['Koding', 'window'] >>> kw.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
What Next?
Python Data Structures
Advertisement