Strings concepts in python part 1

By
What is Python strings?
  • Python string is a collection of Unicode characters.
  • String is a popular data type of python language.
  • Python allow us to create the string by using single quotes,double quotes or triple quotes.
  • Strings can be created by enclosing the the character or the sequence of characters in the quotes(single,double or triple ) in python language.
We can create a string in python in following way as given below:-

    1.) Python strings can be enclosed in single quotes.
    e.g.
    var='msdotnet'
    print("single quote string=",var)
    
    Output:-
    single quote string= msdotnet
    2.) Python strings can be enclosed in double quotes.
    e.g.
    var="Hello friends"
    print("double quote string=",var)
    
    Output:-
    double quote string= Hello friends
    3.) Python strings can be enclosed in triple quotes.
    e.g.
    var1='''Hello friends'''
    var2="""Welcome students """
    print("triple quote string=",var1)
    print("triple quote string=",var2)
    
    Output:-
    triple quote string= Hello friends
    triple quote string= Welcome students 
    4.) To escape the special characters like single quote double quotes or commas etc. by using \ symbol.
    e.g.
    
    str1='We don\'t go to school'
    print("escape the symbols=",str1)
    
    Output:-
    escape the symbols= We don't go to school
    5.) Multi line strings can create in 4 ways as shown below:-
    • By using / symbol at the last line of the string.
    e.g.
    
    str1="welcome to "\
    "my channel "\
    "MNT LAB"
    print("Multi line strings using escape symbols(\)=",str1)
    
    Output:-
    Multi line strings using escape symbols(\)= welcome to my channel mnt lab
    • Enclosed by triple quotes as shown below:-
    e.g.
    str2='''welcome to 
    my channel 
    MNT LAB'''
    print("Multi line strings using escape triple quotes(single)=",str2)
    
    str3="""welcome to 
    my channel 
    MNT LAB"""
    print("Multi line strings using escape triple quotes(double)=",str3)
    
    Output:-
    Multi line strings using escape triple quotes(single)= welcome to 
    my channel 
    MNT LAB
    Multi line strings using escape triple quotes(double)= welcome to 
    my channel 
    MNT LAB
    • By using small brackets like () as shown below:-
    e.g.
    msg=('Welcome '
         'to '
         'My home')
    print("Multi line strings using small brackets()=",msg)
    
    Output:-
    Multi line strings using small brackets()= Welcome to My home
    • By using join() function to write multi line strings in python language.
    e.g.
    
    str4=''.join(("Hello friends,"
                  "How are you?",
                  "i hope you are well."))
    
    print("Multi line strings using join()function=",str4)
    
    Output:-
    Multi line strings using join()function= Hello friends,How are you?i hope you are well.
    6.) We have two ways to escape the special symbols (' ' , " " etc).
    • Escape them by preceding them with a \ symbol.
    e.g.
    msg1='Hello,\'please don\'t go there'
    print("escape the special symbol:",msg1)
    
    Output:-
    escape the special symbol: Hello,'please don't go there
    • Add r/R before any staring strings in python. It is known as a raw string.
    e.g.
    msg2=r"i din't find him"
    print("escape the special symbol:",msg2)
    
    Output:-
    escape the special symbol: i din't find him
    Accessing the string elements in python
    String elements can be accessed using an index value.Any string in python starts with 0 index value.we can accessed any string in two way in python.
    • Accessing by + index value (forward)
    • Accessing by - index value (backward)
    • Accessing by + index value(forward):-
    e.g.
    var='WELCOME'






    var[0]=W
    var[1]=E
    var[2]=L
    var[3]=C
    var[4]=O
    var[5]=M
    var[6]=E
    
    var='WELCOME'
    str1=var[0]         #returns W
    print("index 0 value=",str1)
    str2=var[1]         #returns E
    print("index 1 value=",str2)
    str3=var[2]         #returns L
    print("index 2 value=",str3)
    str4=var[3]         #returns C
    print("index 3 value=",str4)
    str5=var[4]         #returns O
    print("index 4 value=",str5)
    str6=var[5]         #returns M
    print("index 5 value=",str6)
    str7=var[6]         #returns E
    print("index 6 value=",str7)
    
    Output:-
    index 0 value= W
    index 1 value= E
    index 2 value= L
    index 3 value= C
    index 4 value= O
    index 5 value= M
    index 6 value= E




  • Accessing by - index value(Backward):-
  • e.g.
    var='WELCOME'





    var[-1]=E
    var[-2]=M
    var[-3]=O
    var[-4]=C
    var[-5]=L
    var[-6]=E
    var[-7]=W
    var='WELCOME'
    str1=var[-1]                     #returns E
    print("index -1 value=",str1)
    str2=var[-2]                     #returns M
    print("index -2 value=",str2)
    str3=var[-3]                     #returns O
    print("index -3 value=",str3)
    str4=var[-4]                     #returns C
    print("index -4 value=",str4)
    str5=var[-5]                     #returns L
    print("index -5 value=",str5)
    str6=var[-6]                     #returns E
    print("index -6 value=",str6)
    str7=var[-7]                     #returns W
    print("index -7 value=",str7)
    
    Output:-
    index -1 value= E
    index -2 value= M
    index -3 value= O
    index -4 value= C
    index -5 value= L
    index -6 value= E
    index -7 value= W
    Sub-string in python:-
    A sub-string can be sliced out of a string.The slice operator ([]) is used to access the individual characters of the string.We can use : (colon) operator to access the sub-string from the given python strings.
    • string[start:end] -->It extracts from start to end-1.
    e.g.
    var='WELCOME'
    str1=var[0:4]
    print("Sub-string value=",str1)
    str2=var[1:5]
    print("Sub-string value=",str2)
    str3=var[3:6]
    print("Sub-string value=",str3)
    str4=var[0:7]
    print("Sub-string value=",str4)
    
    Output:-
    Sub-string value= WELC
    Sub-string value= ELCO
    Sub-string value= COM
    Sub-string value= WELCOME
    • string[start:]--> It extracts from start to end.
    e.g.
    var='WELCOME'
    str1=var[0:]
    print("Sub-string value=",str1)
    str2=var[1:]
    print("Sub-string value=",str2)
    str3=var[4:]
    print("Sub-string value=",str3)
    str4=var[6:]
    print("Sub-string value=",str4)
    
    Output:-
    Sub-string value= WELCOME
    Sub-string value= ELCOME
    Sub-string value= OME
    Sub-string value= E
    • string[:end]-->It extracts from start to end-1.
    e.g.
    var='WELCOME'
    str1=var[:1]
    print("Sub-string value=",str1)
    str2=var[:3]
    print("Sub-string value=",str2)
    str3=var[:6]
    print("Sub-string value=",str3)
    str4=var[:7]
    print("Sub-string value=",str4)
    
    Output:-
    Sub-string value= W
    Sub-string value= WEL
    Sub-string value= WELCOM
    Sub-string value= WELCOME
    • string[-start:]-->It extracts from -start (included) to end.
    e.g.
    var='WELCOME'
    str1=var[-1:]
    print("Sub-string value=",str1)
    str2=var[-3:]
    print("Sub-string value=",str2)
    str3=var[-5:]
    print("Sub-string value=",str3)
    str4=var[-7:]
    print("Sub-string value=",str4)
    
    Output:-
    Sub-string value= E
    Sub-string value= OME
    Sub-string value= LCOME
    Sub-string value= WELCOME
    • string[:-end]--> It extracts from beginning to -end-1.
    e.g.
    var='WELCOME'
    str1=var[:-1]
    print("Sub-string value=",str1)
    str2=var[:-3]
    print("Sub-string value=",str2)
    str3=var[:-5]
    print("Sub-string value=",str3)
    str4=var[:-6]
    print("Sub-string value=",str4)
    
    Output:-
    Sub-string value= WELCOM
    Sub-string value= WELC
    Sub-string value= WE
    Sub-string value= W
    Note:-If we use too large index ,its reports an error message.but when we use slice operator then it work fine.That's why we use slice operator in python strings.
    Special Notes:-The mktime() function takes struct_time as a argument and returns a floating point seconds in time.The mktime() function is the inverse function of local time.Here a tuple containing 9 elements as a struct time,this struct time pass to the mktime() function as given below:-
    e.g.
    import time
    t=(2020,5,20,25,2,6,4,58,0)
    local_time=time.mktime(t)
    print("local time is=",local_time)
    
    t1=time.localtime(local_time)
    print("system local time",t1)
    
    t2=time.mktime(t1)
    print("system local time = ",t2)
    
    t3=time.time()
    print("system local time in seconds = ",t3)
    
    Output:-
    local time is= 1590003126.0
    system local time time.struct_time(tm_year=2020, tm_mon=5, tm_mday=21, tm_hour=1, tm_min=2, tm_sec=6, tm_wday=3, tm_yday=142, tm_isdst=0)
    system local time =  1590003126.0
    system local time in seconds =  1588352330.213793
    Watch complete Lecture 12 Video:-

    0 comments:

    Post a Comment

    Powered by Blogger.