Fun way to learn the Python Standard Library


People often ask how to learn Python at the PyATL meetups. They often learn the general basic syntax of Python and are a little lost as what to do next. This post shares a quick and fun way to learn more about programming and Python.

This is the way
Quick Example

Following the way described above, I randomly selected the documentation page for the str type https://docs.python.org/3/library/stdtypes.html#text-sequence-type-str

Strings are pretty fun to mess around. They are also very important as they make up most of the communication in applications. Let’s jump into the fun.

The str() class accepts an object as a parameter and returns a string version of the object. In Python everything[0] is an object. That means things will usually have methods and attributes. One of the special attributes being the magic method __str__ (returns a string representation of an object).

Some of the methods available in the string class are capitalize(), center(), and split(). Let’s play around with them.

# str.capitalize()

name = 'pablo'
name = name.capitalize()
print(name)

# Will output 'Pablo'
# str.center()

name = 'Pablo'
name = name.center(10)
print(name)

# Will output '   Pablo   '
# str.split()

name = 'Pablo'
name.split('P')

# Will output ['', 'ablo']

There’s a bunch more things to do with the str() class that are simple and fun. Are you down for a challenge? Use the methods available in the str() class and write a program that prints out a rectangle made out of asterisks with your name in the center.

'''
*****************
*               *
*               *
*               *
*     Pablo     *
*               *
*               *
*               *
*****************
'''

Happy coding!


Footnotes
[0] Well, almost everything.


,