0% found this document useful (0 votes)
28 views7 pages

Operations On STR Data

The document explains operations on string data in Python, focusing on indexing and slicing. It details how to access individual characters using indexing, including positive and negative indices, and how to extract substrings using various slicing techniques. Additionally, it covers the use of steps in slicing and provides examples to illustrate these concepts.

Uploaded by

Godspell
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views7 pages

Operations On STR Data

The document explains operations on string data in Python, focusing on indexing and slicing. It details how to access individual characters using indexing, including positive and negative indices, and how to extract substrings using various slicing techniques. Additionally, it covers the use of steps in slicing and provides examples to illustrate these concepts.

Uploaded by

Godspell
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 7

=================================================

Operations on str data


=================================================
=>On str data, we can perform types of Operations. They are

1. Indexing
2. Slicing
===============================================================================
1. Indexing
===============================================================================
=>The Purpose of Indexing Concept in Python is that to get Single Value from given
object.
=>Syntax: strobj [ index ]
=>Here Index can be either +Ve or -Ve
=>If we enter Valid Index then PVM gives Corresponding Value of that Index
=>If we enter InValid Index then PVM gives IndexError
--------------------
Examples
-------------------
>>> s="PYTHON"
>>> print(s,type(s))
PYTHON <class 'str'>
>>> print(s[0])-----------------P
>>> print(s[-1])----------------N
>>> print(s[-6])---------------P
>>> print(s[-5])----------------Y
>>> print(s[1])----------------Y
>>> print(s[-2])---------------O
>>> print(s[4])----------------O
>>> print(s[5])----------------N
>>> print(s[15])---------------IndexError: string index out of range
>>> print(s[-11])--------------IndexError: string index out of range
==============================================================================
>>> s="Java Prog"
>>> print(s,type(s))--------------------Java Prog <class 'str'>
>>> print(s[4])-----------Space
OR
>>> s[4]----------------- ' '
>>> s[0]---------------- 'J'
>>> s[1]----------------'a'
>>> len(s)-------------9---->len(strobj) gives Number of Characters in str obj
>>> s[len(s)-1]------------'g'
>>> s[len(s)]--------------------IndexError: string index out of range
>>> s[-len(s)]------------------'J'
-----------------------
>>> s="PYTHON IS AN OOP LANG"
>>> print(s,type(s))-----------PYTHON IS AN OOP LANG <class 'str'>
>>> print(s[len(s)-1])--------G
>>> print(s[-len(s)])----------P
>>> print(s[0])--------------P
===================================================================================
2. Slicing Operations
===================================================================================
=>The process of obtaining Range of Characters or Sub String from given str object
is called Slcing Operation.
-----------------------------------------------------------------------------------
--------------------------------------------------
Syntax1: strobj[BEGIN:END]
-----------------------------------------------------------------------------------
-------------------------------------------------
=>This Syntax gives Range of Characters from BEGIN Index Value END-1 Index Value
Provided BEGIN<END otherwise we never get any result or we get Space as Result.
-----------------------------------------
Examples
-----------------------------------------
>>> s="PYTHON"
>>> print(s,type(s))-------------------------PYTHON <class 'str'>
>>> print(s[0:4])------------------------------PYTH
>>> print(s[2:5])------------------------------THO
>>> print(s[4:6])------------------------------ON
>>> print(s[1:4])------------------------------YTH
>>> print(s[1:6])------------------------------YTHON
>>> print(s[0:6])------------------------------PYTHON
>>> print(s[4:2])------------------------------Space
OR
>>> s[4:2]-------------------------------------- ' '
-------------------------------------------------------------------
>>> s="PYTHON"
>>> print(s,type(s))-------------------------PYTHON <class 'str'>
>>> print(s[-6:-2])----------------------------PYTH
>>> print(s[-5:-1])----------------------------YTHO
>>> print(s[-4:-2])----------------------------TH
>>> print(s[-6:-1])----------------------------PYTHO
>>> print(s[-2:-4])-----------------------------space
(OR)
>>> s[-2:-4]-------------------------------------' '
====================================
Sub Rule: strobj[POSBEG:NEGEND]
====================================
=>This Syntax Gives gives Range of chars from POSBEG Index to NEGEND-1 Index
provided POSBEG>NEGEND (Intersection Area of Data ) NEGBEG>POSEND (Intersection
Area of Data ) otherwise we never get any result or we get Space as Result.
---------------
Examples
---------------
>>> s="PYTHON"
>>> print(s)--------------------PYTHON
>>> s[2:-1]---------------------'THO'
>>> print(s[2:-1])-------------THO
>>> print(s[-1:2])--------------space
(OR)
>>> s[-1:2]--------------------' '
>>> s[1:-1]-------------------'YTHO'
>>> s[2:-2]-------------------'TH'
>>> s[-3:4]--------------------'H'
>>> s[-6:4]--------------------'PYTH'
>>> s[-2:0]--------------------''
>>> s="PYTHON"
>>> print(s)-------------------PYTHON
>>> s[2:-1]---------------------'THO'
>>> s[2:-2]---------------------'TH'
>>> s[2:-3]----------------------'T'
-----------------------------------------------------------------------------------
--------------------------------------------------
Syntax2: strobj[BEGIN : ]
-----------------------------------------------------------------------------------
-------------------------------------------------
=>In this Syntax, we are Specifying BEGIN Index and we did't specify END INDEX
=>If we don't specify END Index then PVM Takes END Index as len(strobj)
OR
=>If we don't specify END Index then PVM Takes Chars from BEGIN INDEX Value to Last
Character
--------------------------------
Examples
--------------------------------
>>> s="PYTHON"
>>> print(s)-----------------------PYTHON
>>> len(s)-------------------------6
>>> s[4:]---------------------------'ON'
>>> s[2:]---------------------------'THON'
>>> s[0:]---------------------------'PYTHON'
>>> s[1:]----------------------------'YTHON'
>>> s[3:]---------------------------'HON'
---------------------------------------
>>> s="PYTHON"
>>> print(s)----------------------PYTHON
>>> s[-6:]-------------------------'PYTHON'
>>> s[-4:]-------------------------'THON'
>>> s[-5:]------------------------'YTHON'
>>> s[-3:]------------------------'HON'
>>> s[-2:]------------------------'ON'
-----------------------------------------
>>> s="PYTHON"
>>> print(s)------------------------PYTHON
>>> s[-6:]---------------------------'PYTHON'
>>> s[-4:]----------------------------'THON'
>>> s[-5:]----------------------------'YTHON'
>>> s[-3:]----------------------------'HON'
>>> s[-2:]-----------------------------'ON'
-----------------------------------------------------------------------------------
--------------------------------------------------
Syntax3: strobj[ :END]
-----------------------------------------------------------------------------------
-------------------------------------------------
=>In this Syntax, we are Specifying END Index and we did't specify BEGIN INDEX
=>If we don't specify BEGIN Index then PVM Takes END Index as 0 OR -len(strobj)
OR
=>If we don't specify BEGIN Index then PVM Takes Chars from First Charcater OR 0th
Character or -len(strobj)th Character Value to END-1 Index Value.
-----------------------------------
Examples:
----------------------------------
>>> s="PYTHON"
>>> print(s)------------------PYTHON
>>> print(s[:4])-------------PYTH
>>> print(s[:3])-------------PYT
>>> print(s[:6])-------------PYTHON
>>> print(s[:-1])------------PYTHO
>>> print(s[:0])------------Space
>>> s[:0]--------------------''
>>> s[:-6]-------------------' '
>>> s="PYTHON"
>>> print(s)--------------------PYTHON
>>> s[:3]------------------------'PYT'
>>> s[:-2]-----------------------'PYTH'
>>> s[:-1]-----------------------'PYTHO'
>>> s="PYTHON"
>>> print(s)---------------------PYTHON
>>> s[:-4]------------------------'PY'
>>> s[:-5]------------------------'P'
>>> s[:-1]-----------------------'PYTHO'
>>> s[:0b0011]----------------'PYT'
>>> s[:True+True]------------'PY'
-----------------------------------------------------------------------------------
--------------------------------------------------
Syntax4: strobj[ : ]
-----------------------------------------------------------------------------------
-------------------------------------------------
=>In this Syntax, we are not Specifying BEGIN and END Indices
=>This Syntax Give Characters from Fisr Character to Last Character (gives Complete
data of str obj)
--------------------
Examples
--------------------
>>> s="PYTHON"
>>> print(s)---------------------PYTHON
>>> s[:]--------------------------'PYTHON'
>>> print(s[:])-----------------PYTHON
>>> print(s[0:6])-------------PYTHON
>>> print(s[0:])--------------PYTHON
>>> s="PYTHON PROG"
>>> print(s)----------------------PYTHON PROG
>>> s[:]----------------------------'PYTHON PROG'
>>> print(s[:])--------------------PYTHON PROG
NOTE: In all the above Syntaxes, We are getting the characters from strobj in
FORWARD DIRECTION with Default Step Value 1.
-----------------------------------------------------------------------------------
-----------------------------------------------------------
Syntax5: strobj[BEGIN:END:STEP]
-----------------------------------------------------------------------------------
-----------------------------------------------------------
RULE1: Here the Values of BEGIN, END and STEP can be either +VE or -VE
-----------
RULE2: If the Value of STEP is +VE then PVM Obtains the Characters from BEGIN Index
to END-1 Index
----------- In FORWARD DIRECTION provided BEGIN INDEX < END INDEX otherwise
otherwise we never
get any result or we get Space as Result.
-----------
RULE 3: If the Value of STEP is -VE then PVM Obtains the Characters from BEGIN
Index to END+1 Index
----------- in BACKWARD DIRECTION provided BEGIN INDEX > END INDEX otherwise
otherwise we never
get any result or we get Space as Result.
-------------
RULE4: In FORWARD DIRECTION, If we specify END INDEX as 0 Then we never
------------ get any result or we get Space as Result.

-------------
RULE4: In BACKWARD DIRECTION, If we specify END INDEX as -1 Then we never
------------ get any result or we get Space as Result.
-----------------------------------------------------------------------------------
--------------------------------------------------
RULE-2
>>> s="PYTHON"
>>> print(s)
PYTHON
>>> print(s[0:4:1])
PYTH
>>> print(s[0:4])
PYTH
>>> print(s[:])
PYTHON
>>> print(s[::])
PYTHON
>>> print(s[::2])
PTO
>>> print(s[0:4:2])
PT
>>> print(s[0:6:3])
PH
>>> print(s[0:6:4])
PO
>>> print(s[1:5:2])
YH
>>> print(s[5:1:2])-------------space
(OR
>>> s[5:1:2]
''
>>> s="PYTHON"
>>> print(s)
PYTHON
>>> print(s[-6:-1:1])
PYTHO
>>> print(s[-6:-1])
PYTHO
>>> print(s[-6:-1:2])
PTO
>>> print(s[-6:-1:3])
PH
>>> print(s[-6:-4:3])
P
-----------------------------------------------------------------------------------
-----------
RULE-3
-----------------------------------------------------------------------------------
-----------
>>> s="PYTHON"
>>> print(s)
PYTHON
>>> s[::-1]
'NOHTYP'
>>> s[5:2:-1]
'NOH'
>>> s[4::-2]
'OTP'
>>> s[5:1:-2]
'NH'
>>> s[::-3]
'NT'
>>> s[::-4]
'NY'
>>> s[-1:-6:-1]
'NOHTY'
>>> s[-1:-7:-1]
'NOHTYP'
>>> s[-1:-7:-2]
'NHY'
>>> s[-6:5:-1]
''
>>> s[2:-1:-1]
''
--------------------------
RULE-4 and RULE-5
-------------------------------------
>>> s="PYTHON"
>>> print(s)
PYTHON
>>> s[3:0:2]
''
>>> s[6:0:3]
''
>>> s[-3:-1:-1]
''
>>> s[-6:-1:-2]
''
>>>
-----------------------------------------------------------------------------------
----------------------------------------------------

>>> s="MADAM"
>>> s[::]==s[::-1]
True
>>> s="LIRIL"
>>> s[::]==s[::-1]
True
>>> s="RACECAR"
>>> s[::]==s[::-1]
True
>>> s="DAD"
>>> s[::]==s[::-1]
True
>>> s="MOM"
>>> s[::]==s[::-1]
True
>>> s="MALAYALAM"
>>> s[::]==s[::-1]
True
>>> s="PYTHON"
>>> s[::]==s[::-1]
False
>>> s="MaDaM"
>>> s[::]==s[::-1]
True
>>> s="Madam"
>>> s[::]==s[::-1]
False
>>> s[::].lower()==s[::-1].lower()
True
>>> s[::].upper()==s[::-1].upper()
True
>>> s="PYTHON"
>>> s[::2]==s[::2][::-1]
False
>>> "PYTHON"[::2]=="PYTHON"[::-1][::-2]
True
>>> "PYTHON"[::-1][::-2]
'PTO'
>>> "PYTHON"[::2]
'PTO'
>>> "KVR"[::-1]!="KVR"[::2]
True
>>>

You might also like