Here is how to create a class in Python.

# Class that converts strings to uppercase
class UppercaseWord:

    def __init__(self, word):
        self.word = word
    def make_upper(self):
        return self.word.upper() 
# Let's instantiate the class 
cap_word1 = UppercaseWord(word='hi there') 

# Let's call the instance method




Think of a Python class as a blueprint that creates multiple object instances which all have the same behaviors. A class is recommended to have only one responsibility. The above simple class is responsible for turning text into uppercase.

A class generally needs to have an __init__ method. When an instance of a class is created, Python executes the __init__ method of the class. Therefore, inside __init__ you may want to put the code that you want to be executed when an instance is created. In other languages, __init__ is referred to as the constructor.

Related HowTos
Deploy a web app to Heroku
Schedule a Python script for execution at a specific time every day
Store Python passwords securely on Windows, Mac, and Linux
Do dictionary comprehension
Do list comprehension
Create a virtual environment
Create a new file
Merge two lists
Extract items from two different lists into one list
Check if a text file is empty
Randomly select an item from a list
Generate a random integer
Break a while loop
Create a pandas DataFrame from a dictionary
Create a pandas DataFrame from a list
Get the last item of a list
Delete a column from a pandas dataframe
Access a column of a pandas dataframe
Make a webpage request
Get the first two characters of a string
Loop through two lists at the same time
Recommended Course

Learn Flask development and learn to build cool apps with our premium Python course on Udemy.

Mailing List

We will keep you Python-motivated if you subscribe below.