====================================
String Handling Part-2
====================================
=>On String Data, we can perform Indexing, Slicing Operations and with these
operations, we can also perform different type of operations by using pre-defined
functions present in str object.
-----------------------------------------------------------------------------------
---------------------------------------------
Pre-defined Functions in str object
-----------------------------------------------------------------------------------
---------------------------------------------
1) capitalize()
-----------------------------------------------------------------------------------
---------------------------------------------
=>This Function is used for capitalizing the first letter First word of a given
Sentence only.
=>Syntax: [Link]()
(OR)
strobj=[Link]()
-----------------
Examples:
-----------------
>>> s="python"
>>> print(s,type(s))-------------------python <class 'str'>
>>> [Link]()--------------------'Python'
>>> s="python is an oop lang"
>>> print(s,type(s))-------------------------python is an oop lang <class 'str'>
>>> [Link]()-----------------------------'Python is an oop lang'
-------------------------------------
>>> s="python"
>>> print(s,type(s))--------------------python <class 'str'>
>>> [Link]()--------------------'Python'
>>> print(s,type(s))----------------python <class 'str'>
>>> s=[Link]()
>>> print(s,type(s))-----------------Python <class 'str'>
-----------------------------------------------------------------------------------
---------------------------------------------
2) title():
-----------------------------------------------------------------------------------
---------------------------------------------
=>This is used for obtaining Title Case of a Given Sentence (OR) Making all words
First
Letters are capital.
Syntax: [Link]()
(OR)
s=[Link]()
------------------
Examples:
------------------
>>> s="python"
>>> print(s,type(s))-------------------python <class 'str'>
>>> [Link]()---------------------'Python'
>>> [Link]()-----------------------------'Python'
----------------------------------------------------------
>>> s="python is an oop lang"
>>> print(s,type(s))------------------python is an oop lang <class 'str'>
>>> [Link]()--------------------'Python is an oop lang'
>>> [Link]()----------------------------'Python Is An Oop Lang'
>>> print(s)----------------------------python is an oop lang
>>> s=[Link]()
>>> print(s)--------------------------Python Is An Oop Lang
-----------------------------------------------------------------------------------
---------------------------------------------
3) index()
-----------------------------------------------------------------------------------
---------------------------------------------
=>This Function obtains Index of the specified Value
=>If the specified value does not exist then we get ValueError
=>Syntax: [Link](Value)
=>Syntax: indexvalue=[Link](value)
Examples:
-----------------
>>> s="python"
>>> [Link]("p")------------------0
>>> [Link]("y")-------------------1
>>> [Link]("o")-----------------4
>>> [Link]("n")----------------5
>>> [Link]("K")----------------ValueError: substring not found
-----------------------------------------------------------------------------------
---------------------------------------------
4) upper()
-----------------------------------------------------------------------------------
---------------------------------------------
=>It is used for converting any type of Str Data into Upper Case.
=>Syntax:- [Link]()
OR
strobj=[Link]()
-----------------
Examples:
=---------------
>>> s="python"
>>> print(s)------------------------------python
>>> [Link]()-----------------------'PYTHON'
>>> s="python is an oop lang"
>>> print(s)---------------------------------python is an oop lang
>>> [Link]()--------------------------------'PYTHON IS AN OOP LANG'
>>> s="Python IS an OOP lang"
>>> print(s)-------------------------------Python IS an OOP lang
>>> [Link]()--------------------------'PYTHON IS AN OOP LANG'
>>> s="AbCdEf"
>>> print(s)------------------------AbCdEf
>>> [Link]()----------------------'ABCDEF'
>>> s="PYTHON"
>>> print(s)--------------------PYTHON
>>> [Link]()-----------------'PYTHON'
>>> s="123"
>>> print(s)------------------123
>>> [Link]()----------------'123'
-----------------------------------------------------------------------------------
---------------------------------------------
5) lower()
-----------------------------------------------------------------------------------
---------------------------------------------
=>It is used for converting any type of Str Data into lower Case.
=>Syntax:- [Link]()
OR
strobj=[Link]()
Examples:
-----------------
>>> s="Data Science"
>>> print(s)--------------Data Science
>>> [Link]()------------'data science'
>>> s="python"
>>> print(s)-------------python
>>> [Link]()-----------'python'
>>> s="PYTHON"
>>> print(s)-------------PYTHON
>>> [Link]()------------'python'
>>> s="PYThon"
>>> print(s)----------PYThon
>>> [Link]()---------'python'
-----------------------------------------------------------------------------------
---------------------------------------------
6) isupper()
-----------------------------------------------------------------------------------
---------------------------------------------
=>This Function returns True provided the given str object data is purely Upper
Case otherwise it returns False.
Syntax: [Link]()
Examples:
-----------------
>>> s="PYTHON"
>>> [Link]()-----------True
>>> s="python"
>>> [Link]()----------False
>>> s="Python"
>>> [Link]()----------False
>>> s="PYThon"
>>> [Link]()----------False
>>> s="123"
>>> [Link]()------------False
>>> s="%$#^&@"
>>> [Link]()-----------False
-----------------------------------------------------------------------------------
---------------------------------------------
7)islower()
-----------------------------------------------------------------------------------
---------------------------------------------
=>This Function returns True provided the given str object data is purely lower
Case otherwise it returns False.
Syntax: [Link]()
-----------------
Examples:
-----------------
>>> s="pythopn"
>>> [Link]()------------True
>>> s="pythOn"
>>> [Link]()------------False
>>> s="PYTHON"
>>> [Link]()-----------False
>>> s="123"
>>> [Link]()----------False
-----------------------------------------------------------------------------------
---------------------------------------------
8) isalpha()
-----------------------------------------------------------------------------------
---------------------------------------------
=>This Function returns True provided str object contains Purely Alphabets
otherwise returns False.
Syntax: [Link]()
-------------------
Examples:
-------------------
>>> s="Ambition"
>>> [Link]()--------------------True
>>> s="Ambition123"
>>> [Link]()-------------------False
>>> s="1234"
>>> [Link]()------------------False
>>> s=" "
>>> [Link]()------------------False
>>> s="#$%^@"
>>> [Link]()-----------------False
>>> s="AaBbZz"
>>> [Link]()----------------True
-----------------------------------------------------------------------------------
---------------------------------------------
9) isdigit()
-----------------------------------------------------------------------------------
---------------------------------------------
=>This Function returns True provided given str object contains purely digits
otherwise returns False
Examples:
--------------------
>>> s="python"
>>> [Link]()------------------False
>>> s="python123"
>>> [Link]()----------------False
>>> s="123"
>>> [Link]()-----------------True
>>> s="123 456"
>>> [Link]()---------------False
>>> s="1_2_3"
>>> [Link]()---------------False
>>> s="123KV"
>>> [Link]()-------------False
-----------------------------------------------------------------------------------
---------------------------------------------
10) isalnum()
-----------------------------------------------------------------------------------
---------------------------------------------
=>This Function returns True provided str object contains either Alpabets OR
Numerics or Alpha-Numerics only otherwise It returns False.
=>Syntax: strobj. isalphanum()
---------------------------
=>Examples:
---------------------------
>>> s="python310"
>>> [Link]()-----------------True
>>> s="python"
>>> [Link]()-----------------True
>>> s="310"
>>> [Link]()-----------------True
>>> s="$python310"
>>> [Link]()-----------------False
>>> s="python 310"
>>> [Link]()----------------False
>>> s="$python3.10"
>>> [Link]()----------------False
>>> s="python3.10"
>>> [Link]()-------------False
-----------------------------------------------------------------------------------
--------------------------------------------
11) isspace()
-----------------------------------------------------------------------------------
--------------------------------------------
=>This Function returns True provided str obj contains purely space otherwise it
returns False.
=>Syntax: [Link]()
------------------------
Examples:
----------------------
>>> s=" "
>>> [Link]()-----------True
>>> s=""
>>> [Link]()--------------False
>>> s="python Prog"
>>> [Link]()-------------False
>>> s="Prasana Laxmi"
>>> [Link]()--------------False
>>> [Link]()-----------False
>>> [Link]() or [Link]()-----------False
-----------------------------------------------------------------------------------
--------------------------------------------
12) split()
-----------------------------------------------------------------------------------
--------------------------------------------
=>This Function is used for splitting the given str object data into different
words base specified delimter ( - _ # % ^ ^ , ; ....etc)
=>The dafeult deleimter is space
=>The Function returns Splitting data in the form of list object
=>Syntax: [Link]("Delimter")
(OR)
[Link]()
(OR)
listobj= [Link]("Delimter")
(OR)
listobj=[Link]()
----------------
Examples:
----------------
>>> s="Python is an oop lang"
>>> print(s)----------------Python is an oop lang
>>> [Link]()----------------['Python', 'is', 'an', 'oop', 'lang']
>>> len([Link]())-----------5
>>> x=[Link]()
>>> print(x,type(x))---------['Python', 'is', 'an', 'oop', 'lang'] <class 'list'>
>>> len(x)---------------5
>>> s="12-09-2022"
>>> print(s)-------------12-09-2022
>>> [Link]("-")----------['12', '09', '2022']
>>> s="12-09-2022"
>>> dob=[Link]("-")
>>> print(dob,type(dob))------------['12', '09', '2022'] <class 'list'>
>>> print("Day",dob[0])----------Day 12
>>> print("Month ",dob[1])---------Month 09
>>> print("Year ",dob[2])----------Year 2022
---------------------------------------------------------
>>> s="Apple#Banana#kiwi/Guava"
>>> words=[Link]("#")
>>> print(words)-----------['Apple', 'Banana', 'kiwi/Guava']
>>> words=[Link]("/")
>>> print(words)------------------['Apple#Banana#kiwi', 'Guava']
-------------------------------------------------------------------------------
13) join():
-------------------------------------------------------------------------------
=>This Function is used for combining or joining list of values from any Iterable
object
=>Syntax: [Link](Iterableobject)
Examples:
------------------------------
>>> lst=["HYD","BANG","AP","DELHI"]
>>> print(lst,type(lst))------------------['HYD', 'BANG', 'AP', 'DELHI'] <class
'list'>
>>> s=""
>>> [Link](lst)---------------'HYDBANGAPDELHI'
>>> s=" "
>>> [Link](lst)------------------'HYD BANG AP DELHI'
-------------------------------------------------------------------
>>> t=("Rossum","is", "Father" "of" ,"Python")
>>> print(t,type(t))
('Rossum', 'is', 'Fatherof', 'Python') <class 'tuple'>
>>> k=" "
>>> [Link](t)
'Rossum is Fatherof Python'
>>> t=("Rossum","is", "Father", "of" ,"Python")
>>> k=" "
>>> [Link](t)
'Rossum is Father of Python'
-----------------------------------------------------------------------------------
----------------------------------
14) startswith():
----------------------
=>The startswith() Function returns True if the string starts with the specified
value, otherwise False.
Examples:
-------------------
>>>s="Python is an oop lang"
>>>[Link]("Python")------------True
>>>[Link]("python")------------False
-----------------------------------------------------------------------------------
----------------------------------
15) endswith():
----------------------
=>The endswith() Function returns True if the string ends with the specified value,
otherwise False.
Examples:
-------------------
>>>s="Python is an oop lang"
>>>[Link]("Python")------------False
>>>[Link]("lang")------------True
-----------------------------------------------------------------------------------
----------------------------------
16) swapcase()
----------------------------
=>Make the lower case letters upper case and the upper case letters lower case:
Examples:
>>>s="PyThOn"
>>>[Link]()--------pYtHoN
-----------------------------------------------------------------------------------
----------------------------------
MISc Examples
-----------------------------------------------------------------------------------
----------------------------------
>>> s="python"
>>> [Link]()
'Python'
>>> s="python is an oop lang"
>>> [Link]()
'Python is an oop lang'
>>> s="python is an oop [Link] is also fun lang"
>>> [Link]()
'Python is an oop [Link] is also fun lang'
>>>
>>> s="python"
>>> [Link]()
'Python'
>>> s="python is an oop lang"
>>> [Link]()
'Python Is An Oop Lang'
>>> s="python is an oop [Link] is also fun lang"
>>> [Link]()
'Python Is An Oop [Link] Is Also Fun Lang'
>>> s="PYTHON"
>>> [Link]()
'Python'
>>> s="PYTHON"
>>> [Link]()
'Python'
>>>
>>>
>>> s="PyThOn"
>>> [Link]()
'pYtHoN'
>>> s="PYThon"
>>> [Link]()
'pytHON'
>>> s="PYTHON"
>>> [Link]()
'python'
>>> s="python"
>>> [Link]()
'PYTHON'
>>> s="12345"
>>> [Link]()
'12345'
>>> s="Python3.11"
>>> [Link]()
'pYTHON3.11'
>>> s="$%^&*()"
>>> [Link]()
'$%^&*()'
>>> s="PYTHON"
>>> [Link]()
'python'
>>> s="PYTHON"
>>> [Link]()
'python'
>>> s="PYThon"
>>> [Link]()
'pytHON'
>>> [Link]()
'python'
>>>
>>> s="python"
>>> [Link]()
'python'
>>> [Link]()
'PYTHON'
>>> s="python"
>>> [Link]()
'PYTHON'
>>> s="PYThon"
>>> [Link]()
'PYTHON'
>>> s="PYThon"
>>> [Link]()
'python'
>>>
>>> s="PYTHON"
>>> [Link]()
True
>>> s="python"
>>> [Link]()
False
>>> s="PYTHon"
>>> [Link]()
False
>>> s="java"
>>> [Link]()
True
>>> s="JAva"
>>> [Link]()
False
>>> [Link]()
False
>>> s="1234"
>>> [Link]()
False
>>> [Link]()
False
>>>
>>>
>>> s="python"
>>> [Link]('p')
0
>>> [Link]('o')
4
>>> [Link]('k')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: substring not found
>>> [Link]('2')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: substring not found
>>> s="python"
>>> [Link]('thon')
2
>>> [Link]('khon')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: substring not found
>>> s="python is an oop lang"
>>> [Link]('is')
7
>>> [Link]('o')
4
>>> [Link]('an')
10
>>> [Link]('10')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: substring not found
>>> [Link]("""an""")
10
>>> s="Apple"
>>> [Link]()
True
>>> s="Apple123"
>>> [Link]()
False
>>> s="Ap ple"
>>> [Link]()
False
>>> s="123"
>>> [Link]()
False
>>> s="Pyth$on"
>>> [Link]()
False
>>> s="PYTHON311"
>>> [Link]()
True
>>> s="PYTHON"
>>> [Link]()
True
>>> s="311"
>>> [Link]()
True
>>> s="PYT HON"
>>> [Link]()
False
>>> s="PYTHON3.11"
>>> [Link]()
False
>>> s="PYTH$on"
>>> [Link]()
False
>>> s="123.56"
>>> [Link]()
False
>>> s="123"
>>> [Link]()
True
>>> s="123.45"
>>> [Link]()
False
>>> s="123$23"
>>> [Link]()
False
>>> s="2"
>>> [Link]()
True
>>> s="32"
>>> [Link]()
True
>>> s="PYTHON311"
>>> [Link]()
False
>>> s=" "
>>> [Link]()
True
>>> s="123 456"
>>> [Link]()
False
>>> s=""
>>> [Link]()
False
>>> s=" "
>>> [Link]()
True
>>>
>>> s="Apple is in red"
>>> [Link]()
['Apple', 'is', 'in', 'red']
>>> x=[Link]()
>>> print(x,type(x))
['Apple', 'is', 'in', 'red'] <class 'list'>
>>> len(x)
4
>>> s="08-07-2023"
>>> print(s)
08-07-2023
>>> x=[Link]("-")
>>> print(x)
['08', '07', '2023']
>>> print("Day=",x[0])
Day= 08
>>> print("Month=",x[1])
Month= 07
>>> print("Year=",x[2])
Year= 2023
>>> s="Apple#Mango#kiwi-Banana"
>>> print(s)
Apple#Mango#kiwi-Banana
>>> x=[Link]("#")
>>> print(x)
['Apple', 'Mango', 'kiwi-Banana']
>>> y=[Link]("-")
>>> print(y)
['Apple#Mango#kiwi', 'Banana']
>>> y[0]
'Apple#Mango#kiwi'
>>> y[0].split("#"]
File "<stdin>", line 1
y[0].split("#"]
^
SyntaxError: closing parenthesis ']' does not match opening parenthesis '('
>>> y[0].split("#")
['Apple', 'Mango', 'kiwi']
>>> y[0:1]=y[0].split("#")
>>> print(y)
['Apple', 'Mango', 'kiwi', 'Banana']
>>>
>>> s="123$456$678$156$"
>>> print(s)
123$456$678$156$
>>> [Link]("$")
['123', '456', '678', '156', '']
>>> s="123$456$678$156"
>>> [Link]("$")
['123', '456', '678', '156']
>>>
>>>
>>>
>>> lst=["apple","mango","kiwi","guava"]
>>> print(lst,type(lst)
... )
['apple', 'mango', 'kiwi', 'guava'] <class 'list'>
>>> k=""
>>> [Link](lst)
'applemangokiwiguava'
>>> print(k)
>>> k
''
>>> lst=["apple","mango","kiwi","guava"]
>>> print(lst,type(lst))
['apple', 'mango', 'kiwi', 'guava'] <class 'list'>
>>> k=""
>>> k=[Link](lst)
>>> print(k)
applemangokiwiguava
>>> k
'applemangokiwiguava'
>>>
>>> lst=["apple","mango","kiwi","guava"]
>>> print(lst,type(lst))
['apple', 'mango', 'kiwi', 'guava'] <class 'list'>
>>> k=" "
>>> k=[Link](lst)
>>> print(k)
apple mango kiwi guava
>>> lst=["Python","is","an","oop","lang"]
>>> k=" "
>>> k=[Link](lst)
>>> print(k)
Python is an oop lang
>>> print(k,type(k))
Python is an oop lang <class 'str'>
>>> [Link]()
['Python', 'is', 'an', 'oop', 'lang']
>>> s=" "
>>> [Link]()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'str' object has no attribute 'isnull'
>>>
>>>
>>> s="Python is an oop lang"
>>> print(s)
Python is an oop lang
>>> [Link]("Python")
True
>>> [Link]("Pyt")
True
>>> [Link]("p")
False
>>> [Link]("p".upper())
True
>>> [Link]("lang")
False
>>> s="Python is an oop lang"
>>> print(s)
Python is an oop lang
>>> [Link]("Python")
False
>>> [Link]("lang")
True
>>> [Link]("la")
False
>>> [Link]("ng")
True
>>> [Link]("n")
False
>>> [Link]("g")
True
>>> [Link]("lang".upper())
False
>>>
-----------------------------------------------------------------------------------
--------------------------------------------