Untitled Document
Strings - Working with Strings !!
1. Program - Declaring Variables Click Here
       #********* Source Code From Website - Mangadaku - visit us at -http://mangadaku.com/ *****      
       # Program To Declare Variables and Printing variables 
       
       #We have hardcode variable my_message which is multiline hence placed in """ quotes
       my_message=""" Mangesh 
                   How are you ?"""

       print(my_message)

       
2. Program - To Print length of a string variable Click Here
       #********* Source Code From Website - Mangadaku - visit us at -http://mangadaku.com/ *****      
       # Program - To Print length of a string variable
       
       my_message = "How Are You ?"
       print(len(my_message))
       
3. Program - To Print a character from a string variable Click Here
       #********* Source Code From Website - Mangadaku - visit us at -http://mangadaku.com/ *****      
       # Program - To Print a character from a string variable 
       # Here we will use index to get the character from string 
        my_var = "Hello World !!" 
        print(my_var[5]) 
        print(my_var[8])
       
4. Program - To print string in a given range Click Here
       #!/bin/ksh 
       #********* Source Code From Website - Mangadaku - visit us at -http://mangadaku.com/ *****      
       # Program - To print string in a given range 
       
       my_str="Hello Mangadaku - How Are You ?"
       print(my_str[5:15])
       
5. Program - To Print String in Lower & Upper case Click Here
       #********* Source Code From Website - Mangadaku - visit us at -http://mangadaku.com/ *****      
       # Program - To Print String in Lower & Upper case
       
        my_str="Hello World" 
       #printing string in lower case
       
       print(my_str.lower())            
       print("----------------")
       
       #printing string in upper case
              
       print(my_str.upper())
       
       
6. Program - To Print count (occurenece of a character ) in a string Click Here
       #********* Source Code From Website - Mangadaku - visit us at -http://mangadaku.com/ *****      
       # Program - To Print  count (occurenece of a character ) in a string
       
        my_str="Hello World" 
       print(my_str.count("l"))            
       print(my_str.count("o"))
 
       
7. Program - To find postion( index) of a character or a word in a string Click Here
       #********* Source Code From Website - Mangadaku - visit us at -http://mangadaku.com/ *****      
       # Program - To find postion( index) of a character or a word in a string
       
        my_str="Hello World" 
       print(my_str.find("world"))            
       print(my_str.find("e"))
 
       
8. Program - Replacing charcater and words in a string Click Here
       #********* Source Code From Website - Mangadaku - visit us at -http://mangadaku.com/ *****      
       # Program - Replacing charcater and words in a string
       #IMP -replace function returns new string after replacing , so we need to store the retun value in new varibale or in existing variable 
       
        my_str="Hello World" 
       my_str1=my_str.replace("World","Mangesh")
       my_str2=my_str.replace("lo","lo,")
       print(my_str1)            
       print(my_str2)
 
       
9. Program - To Concatenate a string Click Here
       #********* Source Code From Website - Mangadaku - visit us at -http://mangadaku.com/ *****      
       # Program - Program - To Concatenate a string
       #Approach - 1  
       
        str1 = "Hello" 
        str2 = "User !!"
       print( str1 + ', ' + str2)            
       
       #Approach - 2  ( using .format method)
       
        str1 = "Hello" 
        str2 = "User !!"
       print("{} {} . Welcome" .format(str1,str2))
 
       
10. fstring Click Here
       #********* Source Code From Website - Mangadaku - visit us at -http://mangadaku.com/ *****      
       # fstring - this new string printing and formating feature is available in python 3.5 and above
       #Example - 1  
       
        str1 = "Hello" 
        str2 = "User !!"
       print(f"{str1} , {str2} . Welcome" )   
       
       #Example - 2 You can run string function inside f string 
        str1 = "Hello" 
        str2 = "User !!"
       print(f"{str1} , {str2.upper()} . Welcome" )         
       
       
11. Additional Info Click Here
       #********* Source Code From Website - Mangadaku - visit us at -http://mangadaku.com/ *****      
       # Additional Info
       #If you want to see what all methods and functions are vaialble on a particular variable then use following
       #method - it will display the list of it 
       
       print(dir(str1))   
       print(help(str.lower))         
       
       
Untitled Document