Printing string ,variable and constant

Photo of author

By Jackson Taylor

Home » Electronics Topics » Python & OpenCv » Printing string ,variable and constant

Printing is the first thing most people learn in programming. It shows you what your program is doing, and it is the quickest way to check that a value is what you expect. In this guide you will learn how to print three basic kinds of values: a string, a variable and a constant. The examples use Python, and the video at the end walks through the same idea.

Program

This is the original example. It uses Python 2 syntax, where print is a statement and does not need brackets:

print 'hello'; #prints a string
a=2;print a;  #prints a variable
print 55;   #prints a constant

Python 2 is no longer supported, so you will almost always use Python 3 today. In Python 3, print is a function and needs brackets. The same program looks like this:

  • print(‘hello’) prints a string.
  • a = 2 on one line, then print(a) on the next, prints a variable.
  • print(55) prints a constant (a fixed number).

The semicolons in the original are not needed in Python. They are allowed, but most Python code leaves them out and puts each statement on its own line.

Hardware Hack

Stop Buying New Batteries for Your Tech

Electronics are expensive, but their power sources don't have to be. Discover the simple engineering trick to recondition dead batteries and bring your gadgets back to 100% capacity.

Watch the Free Video

What the three terms mean

String

A string is text. You write it between single quotes or double quotes, such as ‘hello’ or “hello”. When you print a string, Python shows the text without the quotes.

Variable

A variable is a name that stores a value. In a = 2, the name a holds the number 2. When you print a without quotes, Python prints the value stored in it, not the letter. If you write print(‘a’) with quotes, you get the letter a, because it is now a string.

Constant

A constant is a value that does not change while the program runs. The number 55 typed directly in your code is a fixed value, often called a literal. Python has no built-in way to lock a value, so programmers follow a naming habit instead: they write the name in capital letters, such as MAX_SIZE = 100, and agree not to change it. Printing it works the same as printing any other variable.

See also
OPENING AN IMAGE IN PYTHON

Printing step by step in Python 3

1. Print a string

Put the text inside quotes and pass it to print(). For example, print(‘hello’) displays hello on the screen. Use double quotes when the text itself contains an apostrophe, such as print(“it’s working”).

2. Print a variable

First give the variable a value, then pass its name to print(). Set a = 2, then write print(a), and the screen shows 2. You can change the value later and print again to see the new result.

3. Print a constant

You can print a number directly, as in print(55). Numbers do not need quotes. If you want a named constant, assign it first (TAX_RATE = 5) and then print the name.

Printing more than one thing

You often want to show text and a value together. There are three common ways to do it.

  • Separate the items with commas. print(‘a is’, a) prints the text and the value with a space between them. This works with any type of value.
  • Use an f-string. print(f’a is {a}’) puts the value inside the text. F-strings are available in Python 3.6 and later, and they are usually the easiest to read.
  • Join strings with a plus sign. print(‘a is ‘ + str(a)) works, but the number must be converted to a string with str() first. If you are new to joining text, see our guide to string addition in Python for more detail.

Useful print options

The print() function has a few options that save time:

  • sep changes the separator between items. print(‘a’, ‘b’, sep=’-‘) prints a-b.
  • end changes what comes after the output. By default it is a new line. Setting end=” keeps the next print on the same line.
  • \n inside a string starts a new line, and \t adds a tab.

Common mistakes

  • Missing brackets. Writing print ‘hello’ in Python 3 gives a syntax error. Use print(‘hello’).
  • Missing quotes around text. print(hello) makes Python look for a variable called hello. If none exists, you get a NameError.
  • Quotes around a variable name. print(‘a’) prints the letter, not the value stored in a.
  • Joining text and numbers with a plus sign. ‘age: ‘ + 5 causes a TypeError. Convert the number with str(), or use commas or an f-string.
  • Printing a variable before it has a value. The variable must be assigned on an earlier line, or Python raises a NameError.
  • Mismatched quotes. If you start a string with a single quote, end it with a single quote.
See also
What Does a Negative Voltage Mean? Understanding Electrical Potential in Circuits

Why printing matters

Beyond a first lesson, printing is a simple debugging tool. If a result looks wrong, print the variables at each step and you can see where the value changes. It also helps you learn: type a line, print the result and compare it with what you expected. If you want to go back over the basics of printing, our short page on the basics of printing is a good place to start.

FAQ

Why does print ‘hello’ not work for me?

That is Python 2 syntax. Python 3 needs brackets, so use print(‘hello’). Check which version you are running with python –version in a terminal.

Sustainable Tech

Keep Toxic E-Waste Out of Landfills

Don't throw away your failing laptop, phone, or car batteries. Learn how to recondition them at home in just a few hours. It's better for the planet—and your wallet.

Learn How It Works

What is the difference between printing a variable and printing a string?

A string in quotes is printed exactly as written. A variable name without quotes is replaced by the value it holds. So with a = 2, print(a) shows 2 and print(‘a’) shows a.

Does Python have real constants?

Not in the strict sense. Any variable can be changed. By convention, programmers write constant names in capital letters and leave them alone. Some other languages, such as C, C++ and Java, have keywords that enforce this.

Do I need a semicolon after a print statement?

No. In Python each line ends the statement, so a semicolon is unnecessary. You only need one if you put several statements on the same line, and even then it is better to use separate lines.

Revive Your Dead Electronics

Got a drawer full of dead gadgets or a failing car battery? Instead of paying a premium for replacements, use this simple trick to easily recondition them right from home.

See How It Works