Followers

Wednesday, November 18, 2020

Data Types - Strings

Hello friends, Welcome to our website. Today we will talk about  Strings which is a Sub part of Data Types

To Understand this New Topic You need to read our last post which is related to this topic which is given below :-


String

You already know about strings (as data) in Python. In this section, we shall be talking about Python's data type string. A string data type lets you hold string data, i.e., any number of valid characters into a set of quotation marks. 
In Python 3.x, each character stored in a string is a unicode character. Or in other words, all strings in Python 3.x are sequences of pure Unicode Character. Unicode is a system deigned to represent every character from every language. A string can hold any type of known characters i.e., letters, numbers, special characters, of any known scripted language. 
Following are all legal strings in Python :-
"abcd" , "1234" , '$%^&' , "ŠÆËá" , "??????" , '?????' , "????" , '???' , "??" 

String a a sequence of characters

A Python string is a sequence of characters and each character can be individually accessed using its Index. Let us understand this.
Let us first study the internal structure or composition of Python strings as it will form the basis of all the learning of various string manipulation concepts. Strings in Python are stored as individual characters in contiguous location, with two way index for each location. 
The individual elements of a string are the characters contained in it (stored in contiguous memory location) and as mentioned the characters of a string are given two way index for each location. 
Let us understand this with the help of an illustration as given in figure below :-


From Figure above you can infer that :
  1. Strings in Python are stored by storing each character separately in contiguous locations. 
  2. The characters of the strings are given two way indices :    
  • 0, 1, 2, ...... in the forward direction and
  • -1, -2, -3, .......in the backward direction.

 Thus, you can access any character as <stringname>[<index>] e.g., to access the first character of string name shown in in Fig. 5.1 you'll write name[0], because the index of first character is 0. You may also write name[-6] for the above example i.e., when string name is strong "PYTHON". 

Let us consider another string, say subject = 'Computers'. It will be stored as :

subject,     


                         

Thus,
         subject[0] = 'C'          subject[2] = 'm'             subject[6] = 'e'
        subject[-1] = 's'          subject[-7] = 'm'            subject[-9] = 'C'

Since length of string variable can be determined using function len(<string>), we can say that : 
  • First character of the string is at index 0 or -length 
  • Second character of the string is at index 1 or -(length-1)
  • Second last character of the string is at index (length -2) or -2
  • last character of the string is at index (length -1) or -1
In a string, say name, of length ln, the valid indices are 0, 1, 2....ln-1. That means, if you try to give something like :
     >>> name[ln]
Python will return an error like : 
         Traceback (most recent call last) :
              File "<pyshell#40>, line 1, in <module>
                    name[ln]
         IndexError : string index out of range 




The reason is obvious that in string there is no index equal to the length of the string, thus accessing an element like this causes an error. 
Also, another thing that you must know is that you cannot change the individual letters of a string in place by assignment because strings are immutable and hence item assignment is not supported,i.e., 
      name = 'hello'
  name[0] = 'p'
will cause an error like :
        Traceback (most recent call last):
           File "<pyshell#3>",  line1, in <module>
               name[0] = 'p'
       TypeError : 'str' object does not support item assignment 

However You can assign to a string another string or an expression that returns a string using assignment, e.g,, following statement is valid :
           name = 'hello'
           name = "new"

So with this, we end with this Topic, don't forget to follow me so that you can get the latest updates of my new posts that I upload weekly, I will try my best to upload daily OR you can Sign up for Newsletters at the top of the Blog and that will give you E-mail updates of my New Post.
Jai Hind!!!!

Vande Mataram!!! 

No comments:

Post a Comment

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