Followers

Latest techys

Part4- Python Fundamentals | Start from the basics


If u fell any difficulty in watching the content, try to reduce the size of this page or rotate your portable device if you are using it




In the previous article we learnt about STRING LITERALS. Today we shall about types of string literals and size of strings. So let's start.


String types in Python:

    Python allows you to have two string types:

    1. Single-line strings (Basic strings): Single-line strings are those strings that you created by enclosing text in single quotes(' ') or double quotes(" ") i.e. they must terminate in one line. If typed in another line will surely give an error. To understand this, lets try to type the following in the IDLE window:

>>> text1='Hello

        world'


Python will show you an error when you click Enter key after typing Hello.




The reason behind this error is quite clear - Python by default creates single-line strings with both single or double quotes. So, if the quotation is not closed in the same line at the end, then Python shows this error.  



    2. Multi-line string:Sometimes you may need to store some text spread across multiple lines as one single string, for that Python offers multiline strings.

Multi-line strings can be created in two ways:

(a) By adding a backslash at the end of normal single quote or double quote string: In normal strings, just add a backslash( \ ) at the end before pressing the Enter key so that, you may be able to type the same string in another line.


    For instance:

                        >>> text1='Hello\

                        world'

                            This syntax(code in python) will give the output as:



(b) By typing the text in triple quotes : Python allows you to type multiline text strings by enclosing them in triple quotes(both triple apostrophe or triple quotes will work). 

*There is no need of a backslash at the end of a line.

    For example:
                        
                        >>> text1='''Hello
                         world'''

This syntax will the output:



Size of Strings
    Python determines the size of a string as the count of characters in a single-line string. For example, size of string "abc" is 3 and of 'Vishnu' is 6. But if your string literal has an escape sequence contained within it, then it is also counted as one character. 

        Consider some examples below:

'\\'                                Size is 1    (\\ is an escape sequence to represent backslash)

"abc"                            Size is 3 
   
'\ab'                              Size is 2    (\a is an escape sequence thus counted as one single bit)

"Seema\'s pen"           Size  is 11   (\' has been considered as a single bit used for typing                                                               apostrophe('))

"Amy's"                       Size is 4      (apostrophe is not counted as Python allows a single quote                                                         without any escape sequence)  
 

    For multiline strings created with triple quotes, while calculating size, the EOL (End-Of- Line) character at the end of the line is also counted in the size.



     For example: 
                
                    For the syntax below

the size of the string is 5 as the Enter keys are considered as EOL and are counted. 



                      For the syntax below



the size of the string is 5 only as backslashes at the end of a line in multiline string is not counted.




We shall learn about Numeric Literals in the next article.





No comments