Python Tutorial

Here you can learn about the basics of Python for free.

Wednesday, July 1, 2020

String Literals
















The text encoded in quotes forms a string literal in Python.
The text may be surrounded by Double , Single or Triple quotes
For example:

'Hello' , '22'   ------ single quotes
"Python", "3,99" ------double quotes , etc

>>String Types in Python:
1.Single -line Strings :
                                  The string surrounded by single or double qoutes and must be terminate in one line is called Single-line String.














2.Multiline Strings :
                             String which which may spread in two or three lines as a single string.
    Multiline strinds can be created in two ways:
    (a). By adding backslash at the end of normal single-quote / double-quote.
    (b). By typing the text in triple quotation marks. (No backslash needed at the end o line).













>>Size of Strings:
Python determines the size of a strings as the count of characters in the string.
example:
"Hello"------Size=5
"1262"-------Size=4

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


For Multiline strings created with triple quotes,
Example:
              S='''a ↲
              b ↲  
              c'''












Note-----Size of string S is 5 (Three characters a, b, c  and Two EOL characters that follow characters a and b respectively).

For Multiline strings created with single / double quotes and backslash character at end of line :
Example :
            S2='a\
            b\
            c'
 
Note----Size of string S2 is 3 (only 3 characters, no backslash counted.)