String Manipulations

This Python tutorial gives information about strings and their manipulations .

What are strings ?? 

     Strings are Immutable which means that the content of  a string cannot be changed by substitution. 
     Anything enclosed in '......' (single quotes) is a string. Then what is this "......".Ok both are the one and the same.By default the python interpreter displays the string within single quotes all the time except at the cases when the content of the string contains double quotes.eg.'\"' will give an output  ' " ' however '' \' '' will print '' ' '' in the output .

(Note try executing all these instruction in the python shell)

Wait wait is it the number of ways strings can be defined nanana...... there is one more way of expressing strings oops sorry two more        '''.....          and       """....           .
                                                                                     .....                            ....
                                                                                     .....'''                          ...."""
and so these are the two more ways how a string can be defined .Just simple a set of characters enclosed in triple quotes will actually show each and every line in the same order as typed ie with the feature that each and every line will be in the same same position including the line position in the output string. And this is how strings work.

One can very obviously have  quotes enclosed inside the quotes of a string but there is a restriction that each quote inside a quote must be escape characters.

One important point is that if we use an \ at the end of the line the caharacter of the \n will be of no effect i.e.     """....\           .will produce a two line string in the output.
                                       ....
                                       .....'''

String Concatenation

     "hai" "nono" will combine the strings 'hainono'.
     "hai" +"nono" will combine the strings 'hainono'.
      a="hai" b="nono" and a+b will produce 'hainono'.
      however will
      a+'nono' will produce annnnnnnnnnnnnnnn.....................  error 
Why because a is an variable and 'nono ' is a string and not because the single quotes we used because string is just a set of characters and not a set of character in double quotes.

The star character repeats the character the described number of times
a*3 will print 'haihaihai' this is how strings work
a**3 will give error

Assigining strings to variables 


Thus the previous example proves that we could fairly assign the string to the variable with the help of = operator. 

So cant we access the strings simply without the use of a variable ???
     oh, Very obviously but with a restriction,we can use the __doc__ in the script window in order to access the first triple quoted string.

What are escape characters??

     Let me explain with an example we know that '....' is used to make a character string
character . However there Maybe a case when I would like to have the quotes inside the string quotes in that case i dont want the '......' quote to function as a quote that would actually convert the character as a string instead as a character to be displayed in the output in that case we make use of the \' ot \" in order to escape the character from its functionality and theses are escape sequence .
     However the print() gives a string more raedability hence print(' " \" " ') will print " " "
where as if we use the normal console window and type the characters we would probably get " \" " and this is how the print statement provides more readability.

Raw data

     Raw data are equivalent to placing \ before a backslash so that we could prevent the back slash from making the character next to it an escape character.

Strings and their indices

     A string and lists will be approximately similar, what does it meanby similarity ???

Eg : Assume the string ''string'' assigned to i hence i[0] will give a string 's' which is the first letter of the string assigned to i and the string can also be scaled as if the list can just like i[0:5] will give stri and g will not be printed in the output string .

If a whole string copy has to be created then i[:] 

if i[n:m] then the output will be the characters in the string starting from the n th character to the m-1 th character.

how is a string indexed there are two ways they are 
1st way  S T  R  I   N  G 
              0  1  2  3   4   5
and the other way would be 
 S  T  R  I  N  G
-6 -5 -4 -3 -2 -1

But note that when you want to scale or extract a part of the string make sure you should scale the string in a lower inder to higher index order or else you would be errored.


Comments

Popular Posts