You are on page 1of 6

Python Class 17

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

>>> str="hello every one welcome to the class"


>>> x=str.startswith('hello',0)
>>> print(x)
True
>>>
>>> x=str.startswith('hello')
>>> print(x)
True
>>>
>>>
>>> x=str.startswith('hello',5,8)
>>> print(x)
False
>>>
>>>
>>> x=str.startswith(' eve',5,8)
>>> print(x)
False
>>>
>>> x=str.startswith(' eve',5,9)
>>> print(x)
True
>>>
>>>
>>>
>>>
>>>
>>> str="Welcome to Python Programming"
>>> x=str.endswith('Programming')
>>> print(x)
True
>>>
>>>
>>>
>>> x=str.endswith('Programming',10)
>>> print(x)
True
>>>
>>>
>>> x=str.endswith('hi')
>>> print(x)
False
>>>
>>>
>>>

>>> str="Hello Everyone Welcome All"


>>> print(str.find("Everyone"))
6
>>>
>>>
>>> print(str.find('e'))
1
>>>
>>>
>>> print(str.find('e',5))
8
>>>
>>> print(str.find('e',100))
-1
>>>
>>>
>>> print(str.index("Everyone"))
6
>>> print(str.index('e'))
1
>>> print(str.index('e',5))
8
>>> print(str.index('e',100))
Traceback (most recent call last):
File "<pyshell#51>", line 1, in <module>
print(str.index('e',100))
ValueError: substring not found
>>>

Note:
~~~~~~~~~~

In the given string the duplicate character(s) are existed then it will
return first occurrence of the substring index position will be return.

In find(), or rfind() the index position is available or valid then it will


return -1

>>> str="This is Python Programming"


>>> print(str.rfind('is'))
5
>>>
>>> print(str.find('is'))
2
>>>
>>>
>>>
>>> str="Hello World this is Artificial Intelligence"
>>> print(str.find('is'))
14
>>>
>>>
>>> print(str.rfind('is'))
17
>>>
>>>
>>> str="Where there is a will there is a way"
>>> print(str.find('is'))
12
>>> print(str.rfind('is'))
28
>>> print(str.find('is',100))
-1
>>> print(str.rfind('is',100))
-1
>>>
>>> print(str.index('is'))
12
>>> print(str.rindex('is'))
28
>>> print(str.index('is',100))
Traceback (most recent call last):
File "<pyshell#74>", line 1, in <module>
print(str.index('is',100))
ValueError: substring not found
>>>
>>>
>>> print(str.rindex('is',100))
Traceback (most recent call last):
File "<pyshell#77>", line 1, in <module>
print(str.rindex('is',100))
ValueError: substring not found
>>>

>>> str="*************Welcome***************"
>>> print(str)
*************Welcome***************
>>> print(str.lstrip('*'))
Welcome***************
>>>
>>>
>>>
>>> print(str.lstrip("Welcome"))
*************Welcome***************
>>>
>>>
>>> str="Welcome************Welcome***********Welcome"
>>> print(str.lstrip("Welcome")
)
************Welcome***********Welcome
>>>
>>>
>>>
>>> str="------------Hello World-------------"
>>> print(str.rstrip('-'))
------------Hello World
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>> str=" Computer Programming "
>>> print(str)
Computer Programming
>>>
>>>
>>> print(str.lstrip())
Computer Programming
>>>
>>>

>>>
>>>
>>> print(str.rstrip())
Computer Programming
>>>
>>>
>>>
>>>
>>> str="$$$$$$$$$$$$$$$$$Welcome$$$$$$$$$$$$$$$$$$$$"
>>>
>>>
>>>
>>> print(str)
$$$$$$$$$$$$$$$$$Welcome$$$$$$$$$$$$$$$$$$$$
>>>
>>>
>>>
>>> print(str.lstrip('$'))
Welcome$$$$$$$$$$$$$$$$$$$$
>>>
>>>
>>> print(str.rstrip('$'))
$$$$$$$$$$$$$$$$$Welcome
>>>
>>> print(str.strip('$'))
Welcome
>>>
>>>
>>>
>>>
>>> str=" Hi "
>>>
>>>
>>>
>>> print(str)
Hi
>>>
>>>
>>> print(str.lstrip())
Hi
>>>
>>>

>>>
>>>
>>> print(str.rstrip())
Hi
>>>
>>>
>>>
>>>
>>> print(str.strip())
Hi

>>> str="Welcome to Python Programming"


>>> print(str.count('e'))
2
>>>
>>>
>>> print(str.count('o'))
4
>>> print(str.count('hello'))
0
>>> print(str.count("Python"))
1
>>>
>>>
>>> str="aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabc"
>>> print(str.count('a'))
43
>>>
>>> str="This is Python Class, this is really Fun and easy"
>>> print(str.replace('is','was'))
Thwas was Python Class, thwas was really Fun and easy
>>>
>>>
>>> print(str.replace('is','was',2))
Thwas was Python Class, this is really Fun and easy
>>>
>>>

>>> str="Welcome to Python Programming"


>>> print(str.split('e'))
['W', 'lcom', ' to Python Programming']
\
>>>
>>>
>>>
>>> print(str.split('e'))
['W', 'lcom', ' to Python Programming']
>>>
>>>
>>>
>>> print(str.split('to'))
['Welcome ', ' Python Programming']
>>>
>>>
>>> print(str.split())
['Welcome', 'to', 'Python', 'Programming']
>>>
>>>
>>> print(str.split('e',1))
['W', 'lcome to Python Programming']
>>>
>>>
>>>
>>> str="This \n is \n Python \n Programming"
>>> print(str)
This
is
Python
Programming
>>>
>>>
>>> print(str.splitlines())
['This ', ' is ', ' Python ', ' Programming']

>>> sep='-'
>>> str=("Python","Programming","By Experts")
>>> type(str)
<class 'tuple'>
>>>
>>>
>>>
>>> print(sep.join(str))
Python-Programming-By Experts
>>>
>>>

# Program to perform format function

sno=int(input("Enter Your Sno : "))


nm=input("Enter Your Name : ")
job=input("Enter Your Job Role : ")

print("Sno = {}, Name = {}, Job={}".format(sno,nm,job))

print("Sno = {0}, Name={1}, Job={2}".format(sno,nm,job))

print("Sno = {a}, Name = {b}, Job ={c}".format(a=sno,b=nm,c=job))

Output
~~~~~~~~~~~

Enter Your Sno : 111


Enter Your Name : SADAQ
Enter Your Job Role : Corporate IT Trainer
Sno = 111, Name = SADAQ, Job=Corporate IT Trainer
Sno = 111, Name=SADAQ, Job=Corporate IT Trainer
Sno = 111, Name = SADAQ, Job =Corporate IT Trainer
>>>

You might also like