python

Indexing And Slicing Strings

python

Strings use indexes and slices just like lists . >>> spam = 'Hello, world!' >>> spam[0] 'H' >>> spam[-1] '!' >>> spam[0:5] 'Hello' >>> spam[:5] 'Hello' >>> spam[7:] 'world!'

islower()

python

Returns True if string has at least one letter and all characters are lowercase, otherwise returns False. >>> spam 'Hello, world!' >>> spam.islower() False >>> 'abc12345'.islower() True >>> '12345'.islower() False

isupper()

python

Returns True if string has at least one letter and all characters are uppercase, otherwise returns false. >>> spam = 'Hello, world!' >>> spam.isupper() False >>> 'HELLO'.isupper() True >>> '12345'.isupper() False

isX() Methods

python

Similar to islower() and isupper() there are many isX() type methods. isalpha() Returns True if the string consists only of letters and isn’t blank isalnum() Returns True if the string consists only of letters and numbers and is not blank isdecimal() Returns True if the string consists only of numeric characters and is not blank isspace() Returns True if the string consists only of spaces, tabs, and newlines and is not blank istitle() Returns True if the string consists only of words that begin with an uppercase letter followed by only lowercase letters Note, the isX() methods are helpful for input validation. ...

items()

python

The items() method returns tuples of key value pairs. >>> spam {'color': 'red', 'age': 42} >>> for i in spam.items(): ... print(i) ... ('color', 'red') ('age', 42) You can use the multiple assignment trick in a for loop to assign the key and value to separate variables. >>> spam {'color': 'red', 'age': 42} >>> for k, v in spam.items(): ... print('Key: ' + k + ' Value: ' + str(v)) . ...

join()

python

Allows you to join a list of strings into a single string. >>> ', '.join(['cats', 'rats', 'bats']) 'cats, rats, bats'

keys()

python

The keys() method returns a list of keys from a dictionary . @>>> spam {'color': 'red', 'age': 42} @>>> for k in spam.keys(): @... print(k) @... color age Can pass to list() function to create list from output

Keyword Arguments

python

Most arguments are identified by their position in the function call. random.randint(1, 10) # random.randint(10, 1) causes an error. # Order of arguments matters Keyword arguments are identified by the keyword put before them in the function call. Keyword arguments are often optional parameters. See: parameters arguments Example: print('Hello', end='') print('World') Output: HelloWorld Example: >>> print('cats', 'dogs', 'mice') cats dogs mice >>> print('cats', 'dogs', 'mice', sep=',') cats,dogs,mice Note how the print() function automatically separates strings with a ' ‘, but you can specify the field separator with sep=. ...

Learn Python

python

Resources # Curated list of awesome python frameworks, libraries, software and resources Awesome Python Cheat Sheets # Python Cheat Sheet Books # Automate the Boring Stuff Learn Python the Hard Way Python Crash Course by Eric Matthes Recommended by David (has root) Biniek Think Python Introducing Python Video Series # Learn Python with Socratica Recommended by Noah A. ...