String casefold(), capitalize() and title() methods
codeaft@codeaft:~$ python3 ... >>> codeaft="KoDiNgWiNdOw" >>> codeaft.casefold() 'codeaft' >>> codeaft="CODEAFT" >>> codeaft.casefold() 'codeaft' >>> codeaft="codeaft" >>> codeaft.casefold() 'codeaft' >>> codeaft="koding window" >>> codeaft.capitalize() 'Koding window' >>> codeaft.title() 'Koding Window'
String upper(), lower() and swapcase() methods
>>> codeaft="codeaft" >>> codeaft.upper() 'CODEAFT' >>> codeaft="CODEAFT" >>> codeaft.lower() 'codeaft' >>> codeaft="Codeaft" >>> codeaft.swapcase() 'kODINGwINDOW'
String rjust() and ljust() methods
>>> codeaft="Codeaft" >>> codeaft.rjust(20) ' Codeaft' >>> codeaft.ljust(20) 'Codeaft '
String strip(), rstrip(), and lstrip() methods
>>> codeaft=" Codeaft " >>> codeaft.strip() 'Codeaft' >>> codeaft.rstrip() ' Codeaft' >>> codeaft.lstrip() 'Codeaft '
String find() and count() methods
>>> codeaft="Codeaft" >>> codeaft.find("W",0,len(codeaft)) 6 >>> codeaft.find("in",0,len(codeaft)) 3 >>> codeaft.count("in",1,len(codeaft)) 2 >>> codeaft.count("o",1,len(codeaft)) 2
Strings sort() method
>>> codeaft=["K","O","D","I","N","G","W","I","N","D","O","W"] >>> codeaft.sort() >>> codeaft ['D', 'D', 'G', 'I', 'I', 'K', 'N', 'N', 'O', 'O', 'W', 'W'] >>> codeaft=("K","O","D","I","N","G","W","I","N","D","O","W") >>> codeaft.sort()Traceback (most recent call last): File "<stdin>", line 1, in <module> AttributeError: 'tuple' object has no attribute 'sort'
String startswith() and endswith() methods
>>> codeaft="Codeaft" >>> codeaft.startswith("k") False >>> codeaft.startswith("K") True >>> codeaft.startswith("Codeaft") True >>> codeaft.endswith("Codeaft") True >>> codeaft.endswith("ow") True >>> codeaft.endswith("W") False
String zfill() and replace() method
>>> codeaft="Codeaft" >>> len(codeaft) 12 >>> codeaft.zfill(12) 'Codeaft' >>> codeaft.zfill(15) '000Codeaft' >>> codeaft.zfill(0) 'Codeaft' >>> codeaft.replace("i","###") 'Kod###ngW###ndow' >>> codeaft.replace("W","###",1) 'Koding###indow' >>> codeaft.replace("d","###",2) 'Ko###ingWin###ow'
String split() and splitlines() methods
>>> codeaft="Koding\nWindow" >>> codeaft.splitlines() ['Koding', 'Window'] >>> codeaft.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