0% found this document useful (0 votes)
12 views24 pages

Python Session 12 Strings

Uploaded by

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

Python Session 12 Strings

Uploaded by

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

Variables

Data types

Type cast

Print

eval and input

Basic codes

Modules pacakges

conditional

functions

for loop

while loop

In [ ]: - Strings : This I will tell end tom end

- list : 60-40

- Tuple : 100

- dictionary

- sets

- lambda functions

- file handling

Intialization how to write the strings

In built functions

type

min

max

len

sum

sorted

reversed
Concatenation

Index

Mutable/Immutable

Slice

Methods

In [5]: str1='python'
str2="python"
str3='i like "python"'
str4="i like 'python'"
str5="""
hello
how are you
"""

In [9]: print(str1)

python

inbuilt functions

In [16]: type(str1)
print(str1)
len(str1)

python
Out[16]: 6

In [ ]: type()
len()
print()
min()
max()
sum()
sorted()
reversed()

In [18]: str1='apple'
str2='123456'
str3='apple123'

In [22]: print(len(str1))
print(len(str2))
print(len(str3))

5
6
8

In [24]: print(min(str1))
print(min(str2))
print(min(str3))
a
1
1

In [26]: print(max(str1))
print(max(str2))
print(max(str3))

p
6
p

In [ ]: len,min,max

In [28]: sum(str1)

---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
Cell In[28], line 1
----> 1 sum(str1)

TypeError: unsupported operand type(s) for +: 'int' and 'str'

In [30]: print(str2)
sum(str2)

123456
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
Cell In[30], line 2
1 print(str2)
----> 2 sum(str2)

TypeError: unsupported operand type(s) for +: 'int' and 'str'

In [32]: print(str3)
sum(str3)

apple123
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
Cell In[32], line 2
1 print(str3)
----> 2 sum(str3)

TypeError: unsupported operand type(s) for +: 'int' and 'str'

In [34]: 10+'a'

---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
Cell In[34], line 1
----> 1 10+'a'

TypeError: unsupported operand type(s) for +: 'int' and 'str'

In [ ]: sum('apple')
0+'a'

In [36]: 0+'a'+'p'
Out[36]: 'ap'

In [ ]: sum('apple')

In [ ]: def f(a,/,b):
print(a+b)

f(10,20) # w
f(a=10,b=20) # n
f(10,b=20) # w

In [3]: str1='python'
#sorted(iterable, /, *, key=None, reverse=False)
# iterable: anything can be loop
# anything we can print using loop
# for i in 'python'
# print(i)
# strings ,list,tuple,dict all are consider are iterable objects
sorted()

In [5]: print('python')
print([10,20,30])

python
[10, 20, 30]

In [7]: type('python'),type([10])

Out[7]: (str, list)

In [ ]: min()
max()
len()
type()
print()

In [ ]: str1='python'
#sorted(iterable, /, *, key=None, reverse=False)
sorted('python') # works
sorted(iterable='python') # notworks

In [9]: str1='python'
sorted('python')
# by default ascending order
# there is a possibility i can make descending order

Out[9]: ['h', 'n', 'o', 'p', 't', 'y']

In [11]: str1='python'
print(sorted('python')) # ascending
print(sorted('python',reverse=True)) # descening

['h', 'n', 'o', 'p', 't', 'y']


['y', 't', 'p', 'o', 'n', 'h']

reversed
In [15]: str1='python'
reversed(str1)

Out[15]: <reversed at 0x243530b02e0>

In [19]: # for loop


str1='python123'
ans=reversed(str1)
for i in ans:
print(i)

3
2
1
n
o
h
t
y
p

In [21]: str1='python123'
# list type cast
list(reversed(str1)) # int() eval()

Out[21]: ['3', '2', '1', 'n', 'o', 'h', 't', 'y', 'p']

In [ ]: 5am
6am to 7.15am
7.30am to 9am
9am to 10.30am : login
10.30 to 12pm : attend
12.30 -- 1pm : class upload
1pm to 1.30pm
1.30pm to 5pm office
5.30pm to 7pm : class
7pm to 8.30pm : class
9pm
9pm to 10pm 12am 1am

In [ ]: type()
len()
print()
min()
max()
sum()
reversed()
sorted()

# l=[10,20,30,40,50]
# l=['Apple','Ball','Cat']
# l=[10,20,30,40,'Apple','Ball','Cat']

Conactenation

In [ ]: str1='hello'
str2='good morning'
str1+str2
str1*str2
str1-str2
str1/str2

In [24]: str1='hello'
str2='good morning'
str1+str2

Out[24]: 'hellogood morning'

In [26]: str1-str2

---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
Cell In[26], line 1
----> 1 str1-str2

TypeError: unsupported operand type(s) for -: 'str' and 'str'

In [28]: str1/str2

---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
Cell In[28], line 1
----> 1 str1/str2

TypeError: unsupported operand type(s) for /: 'str' and 'str'

In [30]: str1*str2

---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
Cell In[30], line 1
----> 1 str1*str2

TypeError: can't multiply sequence by non-int of type 'str'

In [ ]: TypeError: unsupported operand type(s) for -: 'str' and 'str' # -


TypeError: unsupported operand type(s) for /: 'str' and 'str' # /
TypeError: can't multiply sequence by non-int of type 'str' # *

In [32]: str1='hello'
str1*2 # can multiply sequence by int

Out[32]: 'hellohello'

In [ ]: 2*2: 2+2
'hello'*2 = 'hello'+'hello'

index

In [35]: str1='python'

In [ ]: -6 -5 -4 -3 -2 -1
p y t h o n
0 1 2 3 4 5
index is used to access the letters

in python method or function means ()

in python access anything indicates by []

In [48]: str1='python'
str1[0],str1[1],str1[2],str1[3],str1[4],str1[5]

Out[48]: ('p', 'y', 't', 'h', 'o', 'n')

In [50]: str1[6] # there is no letter asscociated with that number

---------------------------------------------------------------------------
IndexError Traceback (most recent call last)
Cell In[50], line 1
----> 1 str1[6]

IndexError: string index out of range

In [ ]: str1='python'
str1[0]
str1[1]
str1[2]
str1[3]
str1[4]
str1[5]

str1[i] i in range(0,6)

In [56]: # -6 -5 -4 -3 -2 -1
# p y t h o n
# 0 1 2 3 4 5
# 0 to -6
# 1 to -5
# 2 to -4
# i to i-6
# 0 to 0-6
# 1 to 1-6
for i in range(0,6):
print(i,str1[i],i-6)

0 p -6
1 y -5
2 t -4
3 h -3
4 o -2
5 n -1

In [64]: str1='python hai'


n=len(str1)
for i in range(n):
print(i,str1[i],i-n)
0 p -10
1 y -9
2 t -8
3 h -7
4 o -6
5 n -5
6 -4
7 h -3
8 a -2
9 i -1

In [68]: str1='python'
for i in range(len(str1)):
print(str1[i])

p
y
t
h
o
n

In [70]: str1='python'
for i in str1:
print(i)

p
y
t
h
o
n

we are access the letters in two ways

1.using index

index means number

number means range

access through index str1[i]

i means index str1[i] letter

2.Direct access

i in str1

where i indicates direct access the letters

In [ ]: # wap ask the user find the number of 'a' in a given string
# str1='ola ola ola' ans=3

# wap ask the user find the indexes of 'a' in a given string
# str1='ola ola ola' 2 6 10

# wap ask the user find the sum of the indexes of 'a' in a given string
# str1='ola ola ola' 2 +6 +10=18

# wap ask the user find the vowels in a given string


# str1='ola ola ola' o a o a o a

# wap ask the user find the non repated vowels


# str1='ola ola ola' o a

In [75]: # wap ask the user find the number of 'a' in a given string
# str1='ola ola ola' ans=3
str1='ola ola ola'
count=0
for i in str1:
if i=='a':
count=count+1
count

Out[75]: 3

In [81]: str1='ola ola ola'


count=0
for i in range(len(str1)):
if str1[i]=='a':
print(i,str1[i])
count=count+1
count

2 a
6 a
10 a
Out[81]: 3

In [83]: str1='ola ola ola'


count=0
summ=0
for i in range(len(str1)):
if str1[i]=='a':
print(i,str1[i])
count=count+1
summ=summ+i
count,summ

2 a
6 a
10 a
Out[83]: (3, 18)

In [9]: # wap ask the user find the vowels in a given string
# str1='ola ola ola' o a o a o a

str1='ola ola ola'


# 'aeiou' anything can be match
for i in str1:
if i=='a' or i=='e' or i=='i' or i=='o' or i=='u':
print(i)
o
a
o
a
o
a

In [13]: str1='ola ola ola'


s='aeiou'
for i in str1:
if i in s:
print(i)
# i=='a' or i=='e' or i=='i' or i=='o' or i=='u' ====> i in 'aeiou'

o
a
o
a
o
a

In [17]: # wap ask the user find the repated vowels


# str1='ola ola ola'

str1='ola ola ola'


s='aeiou'
s1=''
for i in str1:
if i in s and i not in s1:
print(i)
s1=s1+i
# i='o' 'o' in 'aeiou' 'o' not in '' s=''+'0'='o'
# i='o' if 'o' in 'aeiou' 'o' not in

o
a

Mutable-Immutable

Mutable : Change the letters by using index operations

Immutable : No Change by using index operations

In [22]: str1='welcome' # ====> 'weLcome'


str1[2]='L'

# strings are immutabl in nature

---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
Cell In[22], line 2
1 str1='welcome' # ====> 'weLcome'
----> 2 str1[2]='L'

TypeError: 'str' object does not support item assignment

In [27]: l=[10,20,30,40]
l[2]=300
l
Out[27]: [10, 20, 300, 40]

Slice

Get the substring using index

it is similar to Range in for loop

in range we have start,stop,step

here also we have start:stop:step

range is a function (start,stop,step)

index means access we need [start:stop:step]

In [ ]: # -15 -14 -13 -12 -11 -10 -9 -8 -7 -6 -5 -4 -3 -2 -1


# h a i h o w a r e y o u
# 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14

In [30]: str1='hai how are you'


# postive step stop=last-1
# negative step stop=last+1
str1[2:12]

Out[30]: 'i how are '

In [32]: str1='hai how are you'


str1[2:12:2]

Out[32]: 'ihwae'

In [34]: # -15 -14 -13 -12 -11 -10 -9 -8 -7 -6 -5 -4 -3 -2 -1


# h a i h o w a r e y o u
# 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14

str1='hai how are you'


str1[2:12:-2]

Out[34]: ''

In [38]: # -15 -14 -13 -12 -11 -10 -9 -8 -7 -6 -5 -4 -3 -2 -1


# h a i h o w a r e y o u
# 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14

str1='hai how are you'


str1[12:2:-2]
# str1[start:stop:step]
# direction will provide step value
# step= -2 ====> neagtive
# start=12 last=stop+1=2+1=3

Out[38]: 'yeawh'

In [52]: # -15 -14 -13 -12 -11 -10 -9 -8 -7 -6 -5 -4 -3 -2 -1


# h a i h o w a r e y o u
# 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14

str1='hai how are you'


str1[2:-2:2]
str1[-3:2:-2]

Out[52]: 'yeawh'

In [54]: for i in range(-3,2,-2):


print(i)
# im at 12
# -2 is my back side
# my code says 2 go front side

In [ ]: # -15 -14 -13 -12 -11 -10 -9 -8 -7 -6 -5 -4 -3 -2 -1


# h a i h o w a r e y o u
# 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14

str1='hai how are you'


range(13,-11,-2) # p
str1[13:-11:-2] # p

In [ ]: # -15 -14 -13 -12 -11 -10 -9 -8 -7 -6 -5 -4 -3 -2 -1


# h a i h o w a r e y o u
# 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14

str1='hai how are you'


range(-11,13,-2) # np
str1[4:-4:2] # p

In [66]: # -15 -14 -13 -12 -11 -10 -9 -8 -7 -6 -5 -4 -3 -2 -1


# h a i h o w a r e y o u
# 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14

str1='hai how are you'


str1[:10] # str1[start:stop] 0 to 9
str1[10:] # 10 to last
str1[:] # str1[start:stop]
str1[::] # str1[start:stop:step]
str1[::-1] # str1[stat:stop:-1]
str1[10:100]

Out[66]: 'e you'

In [ ]: - intilaization

- inbuilt : type print len min max sum sorted reversed

- concat

- index : for loop range vs in

- mutable vs immutable ----> we already seen

- slice

In [ ]: str1='python'
list1=[10,20,30,40,50,60]
list2=['A','B','C','D']
list3=[10,20,30,40,50,60,'A','B','C','D']

len(list1)
len(list2)
len(list3)

sum(list1),sum(list2),sum(list3)
max(list1),max(list2),max(list3)
min(list1),min(list2),min(list3)
sorted(list1),sorted(list2),sorted(list3)
reversed(list1),reversed(list2),reversed(list3)
list1+list2
for i in list1:
print(i)

In [68]: list1=[10,20,30,40,50,60]
list2=['A','B','C','D']
list3=[10,20,30,40,50,60,'A','B','C','D']

len(list1)
str1='python'
len(str1)

Out[68]: 6

In [ ]: sum('python')
sum(['p','y','t','h','o','n'])

max([10,'A']) # error
sorted([10,'A']) # comaprining two diff data types

Methods

In [ ]: type()
print()
min()
max()
sorted()
reversed() these all are inbuilt functions
this not only for string
other data types list

In [ ]: strings will have string methods applicable only strings


list will have only list methods only applicable only list

In [4]: import random


random.randint(1,10)
dir(random)

Out[4]: 2

In [6]: dir('')
dir('python')
str1='hello'
dir(str1)
Out[6]: ['__add__',
'__class__',
'__contains__',
'__delattr__',
'__dir__',
'__doc__',
'__eq__',
'__format__',
'__ge__',
'__getattribute__',
'__getitem__',
'__getnewargs__',
'__getstate__',
'__gt__',
'__hash__',
'__init__',
'__init_subclass__',
'__iter__',
'__le__',
'__len__',
'__lt__',
'__mod__',
'__mul__',
'__ne__',
'__new__',
'__reduce__',
'__reduce_ex__',
'__repr__',
'__rmod__',
'__rmul__',
'__setattr__',
'__sizeof__',
'__str__',
'__subclasshook__',
'capitalize',
'casefold',
'center',
'count',
'encode',
'endswith',
'expandtabs',
'find',
'format',
'format_map',
'index',
'isalnum',
'isalpha',
'isascii',
'isdecimal',
'isdigit',
'isidentifier',
'islower',
'isnumeric',
'isprintable',
'isspace',
'istitle',
'isupper',
'join',
'ljust',
'lower',
'lstrip',
'maketrans',
'partition',
'removeprefix',
'removesuffix',
'replace',
'rfind',
'rindex',
'rjust',
'rpartition',
'rsplit',
'rstrip',
'split',
'splitlines',
'startswith',
'strip',
'swapcase',
'title',
'translate',
'upper',
'zfill']

In [ ]: import random str1='python'


dir(random) dir(str1)
# methods methods
random.randint() str1.<meth>

upper

In [9]: str1='python'
str1.upper()

Out[9]: 'PYTHON'

lower

title

capitalize

center

startswith/endswith

lstrip/rstrip/strip

In [12]: str1='HAI How are you'


str1.lower()

Out[12]: 'hai how are you'

In [14]: str1='HAI How are you'


str1.title()

Out[14]: 'Hai How Are You'


In [16]: str1='HAI How are you'
str1.capitalize()

Out[16]: 'Hai how are you'

shift+tab mandatory observe any araguments also read doc string

In [27]: str1='python' # 6
# str1.center(width, fillchar=' ', /)
str2=str1.center(10,'*')
str2,len(str2)

Out[27]: ('**python**', 10)

In [ ]: str1.upper() # upper(str1) X
str1.lower() # lower(str1) X
str1.title() # title(str1) X
str1.capitalize()
str1.center(10,'*')

In [ ]: 'python'.upper() # upper(str1) X
'PYTHON'.lower() # lower(str1) X
'hai How Are you'.title() # title(str1) X
'hai how are you'.capitalize()
'python'.center(10,'*')

In [29]: 'python'.center(6,'*')

Out[29]: 'python'

startswith

In [54]: str1='hai how are you'


#str1.startswith(prefix,start,end)
str1.startswith('hai how are you')
str1.startswith(str1)

Out[54]: True

In [62]: #h a i h o w a r e y o u
#0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
str1='hai how are you'
##str1.startswith(prefix,start,end)
str1.startswith('h') # 'hai how are you'
str1.startswith('h',4) # 'how are you'
str1.startswith('o',4,10) # 'how ar'

Out[62]: False

In [ ]: str1='hai how are you'


##str1.endswith(prefix,start,end)
str1.endswith('h') # 'hai how are you'
str1.endswith('u',4) # 'how are you'
str1.endswith('r',4,10) # 'how ar'
In [64]: str1.endswith('r',4,10) # 'how ar'

# we need to focus on 4 to 10 str1[4:10] : how ar

Out[64]: True

In [72]: #h a i h o w a r e y o u
#0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
str1='hai how are you'
#str1.endswith(suffix,start,end)
str1.endswith('u',4) # str1[4:] : how are you
str1.endswith('o',4,10) # str1[4:10] : how ar

Out[72]: False

In [ ]: ends with 4 index


starts from 4th index

strip

In [77]: str1=' hai how are you'


str1.strip()

Out[77]: 'hai how are you'

In [79]: str1='hai how are you '


str1.strip()

Out[79]: 'hai how are you'

In [81]: str1=' hai how are you '


str1.strip()

Out[81]: 'hai how are you'

In [89]: str1='***hai how are you '


str1.strip().strip('*')

Out[89]: 'hai how are you'

In [91]: str1='hai how *** are you'


str1.split()

Out[91]: ['hai', 'how', '***', 'are', 'you']

In [ ]: #regex operations nlp

count

In [94]: str1='ola ola ola'


# how many 'a' are avaialble
c=0
for i in str1:
if i=='a':
c=c+1
print(c)
3

In [96]: str1='ola ola ola'


#str1.count(sub,start,end)
str1.count('a')

Out[96]: 3

In [98]: str1='hai how are you'


str1.title()

Out[98]: 'Hai How Are You'

In [100… sorted('python')

Out[100… ['h', 'n', 'o', 'p', 't', 'y']

In [106… str1='ola ola ola'


#str1.count(sub,start,end)
str1.count('a')
str1.count('a',3) # str1[3:]
str1.count('a',3,5) # str1[3:5] = ' o'
str1.count('z')

Out[106… 0

In [112… str1='ola ola ola'


str1.count('ola ola')

Out[112… 1

In [114… str1.count('ola')

Out[114… 3

In [122… str1='ola ola ola'


# how many 'a' are avaialble
count=0
for i in range(len(str1)):
#print(i,str1[i:i+3])
if str1[i:i+3]=='ola':
count=count+1
print(count)

Upper/Lower/Casefold

title/capitalize

lstrip/rstrip/strip (which char we want to strip)

startswith/endswith (char,start,end)

count (sub,start,end)

replace
we already know strings are immutable

which means we can not make charcter assign based on index

which means we can not replace or modify a charcter

so the same operation we can do with replace method

In [6]: str1='welcome'
# output : 'weLcome'
#str1.replace(old, new, count=-1)
str1.replace('l','L')

Out[6]: 'weLcome'

In [8]: # Return a copy with all occurrences of substring old replaced by new.
str1='welcome welcome'
str1.replace('l','L')

Out[8]: 'weLcome weLcome'

In [12]: str1='welcome welcome'


str1.replace('l','L',1)

Out[12]: 'weLcome welcome'

In [ ]: str1='welcome'
str1.replace('l','L')
str1='welcome welcome'
str1.replace('l','L')
str1='welcome welcome'
str1.replace('l','L',1)

In [14]: str1='welcome'
str1.replace('z','L')

Out[14]: 'welcome'

In [18]: str1='welcome welcome'


str1.replace('l','L',1)

Out[18]: 'weLcome welcome'

In [20]: 'welcome weLcome'

Out[20]: 'welcome weLcome'

In [30]: str1='welcome welcome welcome'


#'welcome welcome weLcome'
str1.replace("l","L").replace("L","l",2) # okay i like this

Out[30]: 'welcome welcome weLcome'


In [38]: str1='welcome welcome welcome'
str1[::-1].replace('l','L',1)[::-1]

Out[38]: 'welcome welcome weLcome'

In [42]: str1='ola ola ola'


# when i say second 'l' means
# human eyes will search second 'l' from which index
# o l a o l a
# 0 1 2 3 4 5 6
str1.replace('l','L',1)

Out[42]: 'oLa ola ola'

In [50]: ##### SLICE === REPLACE === Conactenation ######


s='ola ola' # trigger 'l'
s1=s[:2] #'ol '
s2=s[2:] #'a ola'
s3=s2.replace('l','L')
s4=s1+s3
s1,s2,s3,s4

Out[50]: ('ol', 'a ola', 'a oLa', 'ola oLa')

In [56]: s='ola ola ola'


s1=s[:6]
s2=s[6:].replace('l','L')
s1+s3

Out[56]: 'ola ola oLa'

In [58]: s='ola ola ola ola'


s1=s[:10]
s2=s[10:].replace('l','L')
s1+s3

Out[58]: 'ola ola ola oLa'

In [60]: s='ola ola' # index =2


s='ola ola ola' # index=6
s='ola ola ola ola' # index=10

# as letters increases we are manually counting


# always remeber a human being doing hard stuff
# there is an alternative method is available

index-find

In [63]: s='ola ola'


# s.index(sub,start,end)
s.index('l')

Out[63]: 1

In [69]: s='ola ola ola ola'


i1=s.index('l') # first 'l' avaibale at index 1 1
i2=s.index('l',2) # second 'l' i need to search from 2 =5
i3=s.index('l',6) #
s.index('l',10)

Out[69]: 13

In [71]: s='ola ola ola ola ola'


i1=s.index('l') # 1
i2=s.index('l',2) # 5
i3=s.index('l',6) # 9
i4=s.index('l',10) # 13
i5=s.index('l',14)

Out[71]: 13

In [ ]: s='ola ola ola ola ola'


i1=s.index('l') # 1
i2=s.index('l',i1+1) # 5
i3=s.index('l',i2+1) # 9
i4=s.index('l',i3+1) # 13
i5=s.index('l',i4+1)

In [73]: s='ola ola ola ola'


n=s.count('l')
print(n)

In [75]: s='ola ola ola ola'


i1=s.index('l')
i2=s.index('l',i1+1)
i3=s.index('l',i2+1)
s1=s[:i3+1] # 3 'l' 3rd index+1
s2=s[i3+1:].replace('l','L')
s1+s3

Out[75]: 'ola ola ola oLa'

In [ ]:
i3=s.index('l',s.index('l',s.index('l') +1) +1)

In [79]: s='ola ola ola ola ola'


i1=s.index('l')
i2=s.index('l',i1+1)
i3=s.index('l',i2+1)
i4=s.index('l',i3+1)
i5=s.index('l',i4+1)
i6=s.index('l',i5+1)
i1,i2,i3,i4,i5,i6

---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
Cell In[79], line 7
5 i4=s.index('l',i3+1)
6 i5=s.index('l',i4+1)
----> 7 i6=s.index('l',i5+1)
8 i1,i2,i3,i4,i5

ValueError: substring not found


In [81]: s='ola ola ola ola ola'
i1=s.find('l')
i2=s.find('l',i1+1)
i3=s.find('l',i2+1)
i4=s.find('l',i3+1)
i5=s.find('l',i4+1)
i6=s.find('l',i5+1)
i1,i2,i3,i4,i5,i6

Out[81]: (1, 5, 9, 13, 17, -1)

In [91]: s1='[email protected]'
s2='[email protected]'
s3='[email protected]'

first=s1.index('.')
secont=s1.index('.',first+1)
at=s1.index('@')
fname=s1[:first]
sname=s1[first+1:at]
cname=s1[at+1:secont]
cname

Out[91]: 'rcb'

In [93]: dir('')
Out[93]: ['__add__',
'__class__',
'__contains__',
'__delattr__',
'__dir__',
'__doc__',
'__eq__',
'__format__',
'__ge__',
'__getattribute__',
'__getitem__',
'__getnewargs__',
'__getstate__',
'__gt__',
'__hash__',
'__init__',
'__init_subclass__',
'__iter__',
'__le__',
'__len__',
'__lt__',
'__mod__',
'__mul__',
'__ne__',
'__new__',
'__reduce__',
'__reduce_ex__',
'__repr__',
'__rmod__',
'__rmul__',
'__setattr__',
'__sizeof__',
'__str__',
'__subclasshook__',
'capitalize',
'casefold',
'center',
'count',
'encode',
'endswith',
'expandtabs',
'find',
'format',
'format_map',
'index',
'isalnum',
'isalpha',
'isascii',
'isdecimal',
'isdigit',
'isidentifier',
'islower',
'isnumeric',
'isprintable',
'isspace',
'istitle',
'isupper',
'join',
'ljust',
'lower',
'lstrip',
'maketrans',
'partition',
'removeprefix',
'removesuffix',
'replace',
'rfind',
'rindex',
'rjust',
'rpartition',
'rsplit',
'rstrip',
'split',
'splitlines',
'startswith',
'strip',
'swapcase',
'title',
'translate',
'upper',
'zfill']

In [ ]: 'isalnum',
'isalpha',
'isascii',
'isdecimal',
'isdigit',
'isidentifier',
'islower',
'isnumeric',
'isprintable',
'isspace',
'istitle',
'isupper',

In [97]: 'Python'.istitle()

Out[97]: True

In [ ]:

You might also like