========================================
Sequence Catagery Data Types
========================================
=>Sequence Catagery Data Types are used for storing Sequence of Values /
Multiple values of same type.
=>We have 4 types Sequence Catagery. They are
1) str
2) bytes
3) bytearray
4) range
==============================================
==
str
==============================================
==
Index:
=>Purpose of str
=>Types of Strings
=>Types String Organization and Notations
=>Operations on Strings
a) Indexing
b) Slicing
==================================================
===========
=>The collection or sequence of characters enclosed within single / double
Quotes is called String (Python) :
Examples: "Python Proghramming" "Guido Van Rossum"
"A" 'A' 'Java Programming'
=>'str' is one of the pre-defined class and treated as Sequence Data Type
=>The Purpose of str data type is that "To store Sequence of values within
Single / Double Quotes or tripple single / double Quotes.
=>We have two types of String data. They are
a) Single Line String Data
b) Multi Line String data
a) Single Line String Data
=>Single Line String Data must be enclosed within Single or Double Quotes or
tripple single / double Quotes.
Examples:-
>>> a="Python Programming"
>>> print(a,type(a)) --------- Python Programming <class 'str'>
>>> b='A'
>>> print(b,type(b)) ------------- A <class 'str'>
>>> c="A"
>>> print(c,type(c)) ----------- A <class 'str'>
>>> d='Java Programming'
>>> print(d,type(d)) ---------- Java Programming <class 'str'>
>>> crs1="Python Programming"
>>> print(crs1,type(crs1)) ----------- Python Programming <class 'str'>
>>> crs2='Python Programming'
>>> print(crs2,type(crs2)) ---------- Python Programming <class 'str'>
>>> crs3="1234567"
>>> print(crs3,type(crs3)) ------------ 1234567 <class 'str'>
>>> crs3="Python3.10"
>>> print(crs3,type(crs3)) ----------- Python3.10 <class 'str'>
>>> x="$%#@&abc&*()"
>>> print(x,type(x)) ----------- $%#@&abc&*() <class 'str'>
>>> x='''A'''
>>> y="""A"""
>>> a="""JAVA"""
>>> b='''PYTHON'''
>>> print(x,type(x)) ------------- A <class 'str'>
>>> print(y,type(y)) ----------- A <class 'str'>
>>> print(a,type(a)) ---------- JAVA <class 'str'>
>>> print(b,type(b)) ------ PYTHON <class 'str'>
=>Hence With Single and double Quotes we can organize / store single line
String data only but organize / store multi line String data.
Examples:
>>> addr1="Guido van Rossum
SyntaxError: unterminated string literal
>>> addr1=' Guido van Rossum
SyntaxError: unterminated string literal
=>To organize multi line string data we must use Tripple Single or tripple double
Quotes.
b) Multi Line String Data
=>Multi Line String Data must be enclosed within tripple single (or tripple
double Quotes.
Examples:
>>> addr1="""Guido van Rossum
... HNO:3-4 Hill side
... CWI ,Python Soft Fund.
... Nether Lands--34567"""
>>> print(addr1,type(addr1))------
Guido van Rossum
HNO:3-4 Hill side
CWI ,Python Soft Fund.
Nether Lands--34567 <class 'str'>
>>> addr2='''James Gosling
... FNO: 45-56 River Side
... Sun Micro Sys,
... USA-12345678'''
>>> print(addr2,type(addr2))------------
James Gosling
FNO: 45-56 River Side
Sun Micro Sys,
USA-12345678 <class 'str'>
>>> x='''A'''
>>> y="""A"""
>>> a="""JAVA"""
>>> b='''PYTHON'''
>>> print(x,type(x)) ------------- A <class 'str'>
>>> print(y,type(y)) ----------- A <class 'str'>
>>> print(a,type(a)) ---------- JAVA <class 'str'>
>>> print(b,type(b)) ------ PYTHON <class 'str'>
==================================X===============
=============
=========================================
Operations on Strings
=========================================
=>On the String data, we can two types of Operations. They are
a) Indexing
b) Slicing
a) Indexing
=>The Process of obtaining one value at a time from given string object is called
Indexing.
=>In Python Programming , we have two types of Indices (or Indexes) . They
are
a) Forward Indexing and starts from Left to Right (0,1,2 ...... )
b) Backward Indexing and starts from Right to Left (-1, -2 -3 ...... )
=>Syntax:
strobj [ Index ]
=>index represents either Possitive and Negative Index.
=>if we enter Invalid Index then we get "IndexError".
Examples:
>>> s="PYTHON"
>>> print(s[0])----------P
>>> print(s[-6])---------P
>>> print(s[-1])--------- N
>>> print(s[5])---------- N
>>> print(s[3])--------- H
>>> print(s[-4])--------- T
>>> print(s[10]) -------- IndexError: string index out of range
>>> print(s[-10]) --- IndexError: string index out of range
==================================================
=============
b) Slicing:
=>The process of obtaining range of characters (or) sub string from given string
object is called String Slicing.
=>Syntax1:- strobj [ Begin : End ]
=>This Syntax obtaing the data from Begin Index Value to End Index-1 Value
provided Begin Index<End Index otherwise we never get Output (Empty).
Examples:
>>> s="PYTHON"
>>> print(s[3:6]) ----------- HON
>>> print(s[6:3])) ----------- empty output
>>> print(s[-6:-3]) --------- PYT
>>> print(s[2:5]) -------- THO
>>> print(s[-4:-1]) ------ THO
=>Syntax2:- strobj [ Begin : ]
=>Here Begin Index is Specified and End Index is not specified. Here pvm takes
end index value as len(strobj)-1
=>This syntax takes the characters from Specified Begin Index to len(strobj)-1
bcoz end index value is automatuically taken as len(strobj)-1.
Examples:
>>> s="PYTHON"
>>> print(s[2:])--------------- THON
>>> print(s[0:])--------------- PYTHON
>>> print(s[4:])-------------- ON
>>> print(s[5:])------------- N
>>> print(s[-1:])----------- N
>>> print(s[-6:])---------- PYTHON
>>> print(s[-3:])---------- HON
>>> print(s[-4:])---------- THON
>>> print(s[-2:])----------- ON
=>Syntax3:- strobj [ :End ]
=>Here Begin Index is not Specified and End Index is specified. Here pvm takes
Begin index value as Intial Index.
=>This syntax takes the characters from Intial Index to End Index-1 bcoz Begin
index value is automatuically taken as Intial Index.
Examples:
>>> s="PYTHON"
>>> print(s) ----------------- PYTHON
>>> print(s[:4])----------- PYTH
>>> print(s[:5])----------- PYTHO
>>> print(s[:6])----------- PYTHON
>>> print(s[:-3])-------- PYT
>>> print(s[:-1])--------- PYTHO
>>> print(s[:-5])---------- P
>>> print(s[:-4])-------- PY
=>Syntax4:- strobj [ : ]
=>Here Begin Index is not Specified and End Index is also not specified.
=>When we are not specifiying Begin Index and End Index PVM takes Intial
Index as Begin Index and len(strobj)-1 as End Index.
Examples:
>>> s="PYTHON"
>>> print(s) ---------- PYTHON
>>> print(s[:]) --------- PYTHON
>>> print(s[:6])--------- PYTHON
>>> print(s[-6:])--------PYTHON
>>> print(s[100:200]) ---------- NO OUTPUT
>>> print(s[0:200]) ------- PYTHON
>>> print(s[0:])--------- PYTHON
>>> print(s[-6:])--------- PYTHON
=>Syntax5:- strobj [ Begin : End : Step ]
Rule-1:- Here the Index values of Begin, End and Step can be Possitive and
Negative .
Rule-2:- If the value of STEP is POSITIVE then PVM Takes the characters from
Begin Index to End Index-1 by Step Interval values in Forward Direction
provided Begin Index<End Index.
Rule-3: If the value of STEP is NEGATIVE then PVM Takes the characters from
Begin Index to End Index+1 by Step Interval values in Backward Direction
provided Begin Index>End Index.
Rule-4: In Forward Direction if the end Index is 0 then we never get any output.
Rule-5: In Back Direction if the end Index is -1 then we never get any output.
Examples ---------- RULE-2
>>> s="PYTHON"
>>> print(s)
PYTHON
>>> print(s[0:6:2])
PTO
>>> print(s[0:6:3])
PH
>>> print(s[-6:-1:2])
PTO
>>> print(s[-6::2])
PTO
>>> print(s[0:4:3])
PH
>>> print(s[::3])
PH
>>> print(s[::4])
PO
>>> print(s[::1])
PYTHON
>>> print(s[::])
PYTHON
>>> s="PYTHON" ----------------- RULE-3
>>> print(s) ------------ PYTHON
>>> print(s[6:0:-2]) -------- NHY
>>> print(s[::-2])---------- NHY
>>> print(s[5:0:-3]) ------- NT
>>> print(s[5:0:-3]) -------- NT
>>> print(s[5:0:-2]) --------- NHY
>>> print(s[::-4])-------- NY
>>> print(s[-1:-6:-2]) --------- NHY
>>> print(s[-1:-6:-1]) ---------- NOHTY
>>> print(s[::-1])----------- NOHTYP
>>> print(s[::1])---------- PYTHON
>>> print(s[::]) ------------ PYTHON
>>> print(s[::-1])---------- NOHTYP
>>> s="LIRIL"
>>> print(s[::]) ---------- LIRIL
>>> print(s[::-1])--------- LIRIL
>>> "JAVA"[::-1] ---------- 'AVAJ'
>>> "MADAM"[::-1]=="MADAM"[::1] ---------- True
>>> "JAVA"[::-1]=="JAVA"[::1] ----------- False
>>> s="PYTHON"
>>> print(s[:0:2])-----------NO output---RULE-4
>>> print(s[:0:1])------------NO output---RULE-4
>>> print(s[:-1:-1])---------NO output --- RULE-5
>>> print(s[:-1:-2])--------NO output-------RULE-5
==========================X=======================
===========