Followers

Wednesday, November 20, 2019

Literals (Token) - String Literals

Literals

Today we will talk about Literals, a type of Tokens. Also you will learn about Literals five different types.

Literals (often referred to as constant values) are data items that have a fixed value. Python allow several kinds of Literals :
  • String Literals
  • Numeric Literals
  • Boolean Literals
  • Special Literals
  • Literal Collection

String Literals 

Basically String Literals in Python are those Literals which are enclosed in quotes. For Example 'a', "Humans", "Water", 'Rope', 'abc' are all Examples of String Literals. Unlike Other Programming Languages, both single character enclosed in quotes such as 'a', "a" and Double characters enclosed in quotes such as"ab", 'abc', are treated as String Literals. 
As you can notice, one can Form String Literals by enclosing text in both forms of quote - Single Quotes or Double Quotes. Following are some valid String Literals in Python :
'Amit',        "Google Home",         "80170",           'Amit_12'
Python allows you to have certain Non Graphic-characters in String values. Non Graphic Characters are those character that cannot be typed directly from keyboard. Example; backspace, tabs, carriage, return etc. No characters is typed when these keys are pressed, only some action takes place) These Non Graphic-Characters can be represented by using escape sequences. An escape sequence is represented by a Backslash(/) followed by one or more character.
In The above Image Non Graphic character is named as Non Printing Character.

 Types Of String Literals 

There are two types of Strings Literals in Python. they are as follows :
  • Single Line Strings (Basic Strings): The strings that you type in single quotes or double quotes are normally Single Line Strings i.e., they must be in one line. Let us see an example to understand this,
            >>> Stud = 'Hello↲
                        World'
            >>>print(Stud)
Then this will show you an error. You can Check it out by Yourself, just CLICK HERE and typed the above code in the compiler. You will see that it will show you error like this :
                                 File "<string>", line 3
                        Stud = 'hello
                                      ^
                   SyntaxError: EOL while scanning string literal
            
The reason behind for the above error is quite clear - Python by default creates single line strings with both single and double quotes. So, if at end of a line, there is no closing quotation mark for an opened quotation mark, Python shows an error.
  • Multi line string : Sometimes we need to store some text spread across multiple lines as one single string. And for that Multi line strings are used. Multi line strings are created either by Triple Quotes or by adding a Backslash at the end of the normal single quote or double quote strings. Let us use these and study these in brief.
            (i)By adding a Backslash: In normal strings, just add a backslash in the end before             pressing Enter to continue typing text on the next line. For example,
                   >>>Stud1 = ' Hello\
                          World '
                   >>>print(stud)
Output:      
             HelloWorld
You can also try this in the compiler, just CLICK HERE . You will find that the same output will come. 
         (ii)By typing the text in Triple quotation mark: Python allows you to type multiline                  string by enclosing them in triple quotation marks (Both Triple Single Quotes                 or Triple Double Quotes). Let us see By an Example,
                   >>>Stud_1 = '''Hello
                           World'''.
                   >>>Stud_2 = """Hello
                          World"""
                   >>>print(Stud_1,Stud_2)                
Again telling it, just try this in the compiler , just CLICK HERE . Don't type the print Function again and again, just write the print Function and then the first code that has to be followed by the second code separating both by a Comma i.e., print(code1,code2,code3,.....), But remember one thing that the Output will be shown in a Single Line and not One after the other. So that's an exception. 

So this is all for today, see you all in the next post. Don't forget to Follow to get my post updates and Share it so that your Friends also can get a chance to learn Python.
JAI HIND!!! 
VANDE MATARAM!!!!!

              

No comments:

Post a Comment

If you have any doubts, please do let me Know.