Python Slicing of Strings:(PYTHON FUNDAMENTALS)

Created with Sketch.

Python Slicing of Strings:

1) slice means a piece
2) [ ] operator is called slice operator, which can be used to retrieve parts of String.
3) In Python Strings follows zero based index.
4) The index can be either +ve or -ve.
5) +ve index means forward direction from Left to Right
6) -ve index means backward direction from Right to Left

 

 >>> s="durga" 
 >>> s[0] 
 'd' 
 >>> s[1] 
 'u' 
 >>> s[-1] 
 'a' 
 >>> s[40]

 

 

>>> s[1:40] 
 'urga' 
 >>> s[1:] 
 'urga' 
 >>> s[:4] 
 'durg' 
 >>> s[:] 
 'durga' 
 >>> 
 
 >>> s*3 
 'durgadurgadurga' 
 
 >>> len(s) 
 5

Note:
1) In Python the following data types are considered as Fundamental Data types

  •  int
  •  float
  •  complex
  •  bool
  •  str


2) In Python, we can represent char values also by using str type and explicitly char type is not available.

 

1) >>> c='a' 
2) >>> type(c) 
3) <class 'str'>

 

3) long Data Type is available in Python2 but not in Python3. In Python3 long values also we can represent by using int type only.


4) In Python we can present char Value also by using str Type and explicitly char Type is not available.

Leave a Reply

Your email address will not be published. Required fields are marked *