Variables in Python can hold text and numbers. For example:

The variable names are on the left and the values on the right. Once a variable is assigned, it can be used in other places of the program.

Related Course:
Complete Python Bootcamp: Go from zero to hero in Python

In the example above, we have three variables: x, price and word.

Note: Variables may not contain spaces or special characters.

Text in variables

Text variables may be defined in 3 ways:

The type depends on what you prefer. 


Once defined variables can be replaced or modified:

Python supports the operators +, -, / and * as well as brackets.  Variables may be shown on the screen using the print statement.

The first output of the program above is simply the raw value of the variables. If you want to print a more detailed message like “x = 5”, use the line ‘print(“x = ” + str(x))’. This str() function converts the numeric variable to text.

Previous Post
Next Post


  • Sejal says:

    What does the command ‘print(“x=”+str(x))’ print ?

    • CodeNinja says:

      It prints the value inside x and a text description: “x=..”

  • jordan says:

    >>> x=5
    >>> print(“x = ” + str(x))
    Output: x = 5 its not getting printed as string

    • CodeNinja says:

      Run the code from a file (

      1. save as text file (
      2. python

  • taranarmo says:

    I suppose that by “we have tree variables” you meant “we have three variables”

Leave a Reply