Python: Concept of String

Concept of String in Python


A String is a sequence of characters which are generally symbols. 
Our Computer system is made up in such a way that it only reads binary numbers i.e. '0' or '1'. 
But still we can see characters o our computer screen. This is because of the internal conversion in which these characters are stored and manipulated as the combination of 0's and 1's. This conversion of characters to binary number is called as Encoding.      

In Python, string is a sequence of Unicode (a type of encoding) characters.  

Creating a String

In Python, strings can be created by enclosing characters in single or double quotes and for multi line string triple quote is used.

Syntax: 
string = 'Python'
string = "Python"
string = '''Python'''
string = """I Love
                  Python""" 

Accessing characters in String

Characters can be accessed individually using indexing and range of characters can be accessed using slicing.  

Indexing starts from zero and if we try to access a character out of index range it gives us an error i.e. IndexError.

In python negative indexing is also allowed.Here index -1 refers to last item, -2 refers to second last item and so on.  
  
The index of -1 refers to the last item, -2 to the second last item and so on. We can access a range of items in a string by using the slicing operator (colon).

 Syntax: 
string = 'Solution For Everyone'
print('string = ', string)                                                                                                                                                                             Output:  
         Solution For Everyone

#first character
print('string[0] = ', string[0])                                                                                                                                                                      Output:  
         S
#last character
print('str[-1] = ', str[-1])                                                                                                                                                                            Output:  
         e
#slicing 2nd to 5th character
print('str[1:5] = ', str[1:5])                                                                                                                                                                        Output:  
         olut
#slicing 6th to 2nd last character
print('str[5:-2] = ', str[5:-2])                                                                                                                                                                      Output:  
         ion For Everyo

Problem Statement 

A Bank ABC incurs compound interest on the loans given to its customers. A customer of the bank wants to know before he borrows the loan the EMI, yearly interest, reduction in the principal amount and the total interest he has to pay for a particular tenure paid by him at the current rate of interest. Write a python script for the above scenario.

No comments:

Powered by Blogger.