Literal Constants 


A Byte of Python 

Go to the Table of Contents. Visit the Gifcom web site.

Just printing 'Hello World' is not enough, is it? You want to do more than that - you want to take some input, manipulate it and get something out of it. We can achieve this in Python using constants and variables.

Literal Constants

An example of a literal constant is a number like 5, 1.23, 9.25e-3 or a string like 'This is a string' or "It's a string!". It is called a literal because it is literal - you 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.

Numbers

Numbers in Python are of four types - integers, long integers, floating point and complex numbers.

  • Examples of integers are 2 which are just whole numbers.

  • Long integers are just bigger whole numbers.

  • Examples of floating point numbers (or floats for short) are 3.23 and 52.3E-4. The E notation indicates powers of 10. In this case, 52.3E-4 means 52.3 * 10-4.

  • Examples of complex numbers are (-5+4j) and (2.3 - 4.6j)

Strings

A string is a sequence of characters. Strings are basically just a bunch of words.

I can almost guarantee that you will be using strings in almost every Python program that you write, so pay attention to the following part. Here's how you use strings in Python:

  • Using Single Quotes (')

    You can specify strings using single quotes such as 'Quote me on this' . All white space i.e. spaces and tabs are preserved as-is.

  • Using Double Quotes (")

    Strings in double quotes work exactly the same way as strings in single quotes. An example is "What's your name?"

  • Using Triple Quotes (''' or """)

    You can specify multi-line strings using triple quotes. You can use single quotes and double quotes freely within the triple quotes. An example is

    				
    '''This is a multi-line string. This is the first line.
    This is the second line.
    "What's your name?," I asked.
    He said "Bond, James Bond."
    '''
    				
    				
  • Escape Sequences

    Suppose, you want to have a string which contains a single quote ('), how will you specify this string? For example, the string is What's your name?. You cannot specify 'What's your name?' because Python will be confused as to where the string starts and ends. So, you will have to specify that this single quote does not indicate the end of the string. This can be done with the help of what is called an escape sequence. You specify the single quote as \' - notice the backslash. Now, you can specify the string as 'What\'s your name?'.

    Another way of specifying this specific string would be "What's your name?" i.e. using double quotes. Similarly, you have to use an escape sequence forusing a double quote itself in a double quoted string. Also, you have to indicate the backslash itself using the escape sequence \\.

    What if you wanted to specify a two-line string? One way is to use a triple-quoted string as shown above or you can use an escape sequence for the newline character - \n to indicate the start of a new line. An example is This is the first line\nThis is the second line . Another useful escape sequence to know is the tab - \t. There are many more escape sequences but I have mentioned only the most useful ones here.

    One thing to note is that in a string, a single backslash at the end of the line indicates that the string is continued in the next line, but no newline is added. For example,

    				
    "This is the first sentence.\
    This is the second sentence."
    				
    				

    is equivalent to "This is the first sentence. This is the second sentence."

  • Raw Strings

    If you need to specify some strings where no special processing such as escape sequences are handled, then what you need is to specify a raw string by prefixing r or R to the string. An example is r"Newlines are indicated by \n".

  • Unicode Strings

    Unicode is a standard way of writing international text. If you want to write text in your native language such as Hindi or Arabic, then you need to have a Unicode-enabled text editor. Similarly, Python allows you to handle Unicode text - all you need to do is prefix u or U. For example, u"This is a Unicode string.".

    Remember to use Unicode strings when you are dealing with text files, especially when you know that the file will contain text written in languages other than English.

  • Strings are immutable

    This means that once you have created a string, you cannot change it. Although this might seem like a bad thing, it really isn't. We will see why this is not a limitation in the various programs that we see later on.

  • String literal concatenation

    If you place two string literals side by side, they are automatically concatenated by Python. For example, 'What\'s' 'your name?' is automatically converted in to "What's your name?".

Note for C/C++ Programmers

There is no separate char data type in Python. There is no real need for it and I am sure you won't miss it.

Note for Perl/PHP Programmers

Remember that single-quoted strings and double-quoted strings are the same - they do not differ in any way.

Note for Regular Expression Users

Always use raw strings when dealing with regular expressions. Otherwise, a lot of backwhacking may be required. For example, backreferences can be referred to as '\\1' or r'\1'.