Python Programming – Constants

You can learn about Introduction to Python Programming Programs with Outputs helped you to understand the language better.

Python Programming – Constants

Constants are the items that represent values directly and whose values cannot be changed during the execution of the program. Fixed values such as numbers, letters, and strings are called constants because their value does not change. String constants use single-quotes (‘) or double-quotes ( ” ). For example:

>>> print(123)
123
>>> print(98.6)
98.6
>>> print(‘Hello world’)
Hello world
>>> VOWELS = “aeiou”

Variable name in all caps is called constant and refers to the value that is not meant to change (i.e., their value is constant).

Let’s Try

What will be the output of the following print state-ments?

>>> print ( 63 )
>>> print ( 75.5 )
>>> print ( ‘ Hello! How are you? ‘ )
>>> name = ” Shiva ”
>>> print ( name )

Constants are valuable to programmers in two ways:
(a) They make the program clearer. Here, the variable name VOWELS can be used anywhere you need the sequence of vowels instead of the string “aeiou”
(b) They save retyping. They are especially useful if you have a long value such as like very long number or string.

  • Note: Use a constant in programs where you have the same, unchanging value used in multiple places. Note that when you create constants use all caps variable name.

Literal Constants

An example of a literal constant is a number, such as 5,1.23, or a string such as ‘This is a string’, or “It’s a string!”. It is called a literal because it will use its value literally. The number 2 always represents itself and nothing else; it is a constant because its value cannot be changed. Hence, all these are referred to as literal constants.

String Literals & String value

A string literal is a sequence of characters surrounded by quotes. You can use single, double, or triple quotes for a string. And, a character literal is a single character surrounded by single or double-quotes.
You get a string value when you call the print() function and run the program.
>>> print(“Hello, World!”)
Hello, World!
Here, the string literal is “Hello, World!”, while the string value is Hello, World! without the quotation marks.

Generally, the string value which you get as the output in the terminal window when you run a Python program does not include quotation marks. But some string values may need to include quota¬tion marks, like when you are quoting a source. Since string literals and string values are not equivalent, it is often necessary to add additional formatting to string literals to ensure that string values have displayed the way in which you intend to.

Quotes and Apostrophes

Since you can use single quotes or double quotes, in Python, it is simple to embed quotes in a string by using double quotes within a string enclosed by single quotes:
‘Viveksays, “Hello!”’
Or, you can use an apostrophe in a string enclosed by double quotes:
“Vivek’s shirt is blue.”
In the way you combine single and double quotes, you can control the display of quotation marks and apostrophes within your strings.
With multiple lines, strings can be grouped into clean and orderly text, formatted as a letter, or used to maintain the line breaks of a poem or song lyrics. Strings on multiple lines can make the text more readable to humans. To create strings that span multiple lines, triple single quotes ‘” or triple double quotes ” ” ” are used to enclose the string.

  • Note: With triple quotes, you can print strings on multiple lines to make text, especially lengthy text, easier to read.

Using Escape characters is another way to format strings. All Escape characters start with the back¬slash (\) combined with another character within a string to format the given string a certain way. By using the escape character \” you are able to use double quotes to enclose a string that includes in the text quoted between double-quotes. Similarly, you can use the escape character \’ to add an apostrophe in a string that is enclosed in single quotes.
>>> print (11 Vivek says, \”Hello! \””)
Vivek says, “Hello!”
>>> print(‘Vivek\’s shirt is blue.’)
Vivek’s shirt is blue.

A raw string tells Python to ignore all formatting within a string, including escape characters. You create a raw string by putting an r in front of the string, just before the beginning quotation mark:
>>> print(r”Vivek says,\”The shirt\’s colour is blue.\M”)
Vivek says,\”The shirt\’s color is blue.\”
By constructing a raw string by using r in front of the given string, you can retain backslashes and other characters that are used as escape characters.

Leave a Reply

Your email address will not be published. Required fields are marked *