Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
11 views

String in Python For Class 11

String is very useful for python programming...
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

String in Python For Class 11

String is very useful for python programming...
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 28

String in Python

• Are enclosed in quotes of any type(single, double or triple)


e.g. ‘a’, ”City”, ’’’States’’’
• Are used to store text type of data
• Are immutable(non-changeable)
• Empty string(0 characters means a pair of quotation
marks only)
•Types:
• Single-line String e.g. name=“Python”
• Multi-line String=“Python\
• Programming”
String in Python
• These are sequence of characters
• Each character having a unique id/index
• The index begin from
• forward direction (0-(length-1))
• backward direction (-1, -2, -3, .. -
length)
0 1 2 3

-4 -3 -2 -1
Traversing in Python
Forward direction (0-(length-1))

Name=“DAV”
for i in Name:
print(i) or print(Name[i])

Output:
D
A
V
-String in Python by Binit Jha 4
Traversing in Python
 Backward direction (-1, -2, -3, .. -length)

Name=“DAV”
leng=len(Name)
for x in range(-1,(-leng-1),-1):
print (Name[x]) #print ('
Name[',x,']:’,Name[x])
Or Output:
for x in range(0,len(Name)): Name[ -3 ]: D
Name[ -2 ]: A
print('Name[',x,']:',Name[x])
Name[ -1 ]: V
-String in Python by Binit Jha 5
Traversing in Python
Program to read a string and display
it in
First character Last character
Second char. Second last char.
And so on…

Cont…

-String in Python by Binit Jha 6


Traversing in Python
Output
name=input("Enter ur Enter ur name:
name:") Python
leng=len(name) P n
i=0 y o
for a in range(-1,(-leng- t h
1),-1): h t
print(name[i],'\ o y
t',name[a]) n P
i=i+1
-String in Python by Binit Jha 7
String Operators
1. Basic Operators
 ‘+’ operator: performs concatenation
e.g. “PYTHON”+“Programming”
Note: You should use single data type as
operands

 ‘*’ operator: performs replication


e.g. print(‘\n’ *10)
“Python” *10 2*3
Note: You should not use both the operands
as string
-String in Python by Binit Jha 8
String Operators
2. Membership Operators
 in
e.g. "on" in "Python“
o/p: True
 not in

e.g. name="Python“
substring="on“
substring not in name
o/p: False
-String in Python by Binit Jha 9
String Operators
3. Comparison Operators
‘is’,‘==’, ‘!=’, ‘<‘, ‘>’, ‘<=‘, ‘>=‘

“ord()” is used to determine the


corresponding ordinal Unicode/ASCII value.
e.g. ord(a)o/p: 65

Note:
‘==’ will work based upon the values
‘is’ keyword compares based upon the
memory location.
-String in Python by Binit Jha 10
String Operators
3. Comparison m='a'
Operators n=input() #a
‘is’,‘==’, ‘!=’, m==n #True
‘<‘, ‘>’, ‘<=‘, m is n #True
‘>=‘

Note: use id()


 m='abc' method to chk the
n=input() #abc memory location.
m==n#True
m is n #False
-String in Python by Binit Jha 11
String Slices
Refers to the contiguous part of
the string, containing some, all
the characters.

-String in Python by Binit Jha 12


String Slices
Index 0 1 2 3 4

J A M E S
NAME

Index-5 -4 -3 -2 -1
e.g.
NAME[1:5] o/p: ‘AMES’
NAME[0:3] o/p: ‘JAM’
NAME[-5:-2] o/p: ‘JAM’
-String in Python by Binit Jha 13
String Functions
1. a.) capitalize() I. b.) title()
Strn=“dav Name=“dav
bariatu” bariatu”
print(strn.capitaliz Name.title()
e()) #Dav Bariatu
Output: ‘Dav
bariatu’
or

‘pYTHON’.capitaliz
e() -String in Python by Binit Jha 15
String Functions
1. c.) istitle()
"dav bariatu".istitle()
False
"Dav Bariatu".istitle()
True
"Dav bariatu".istitle()
False

-String in Python by Binit Jha 16


String Functions
len()

len(“Hi”) #2
Name=“DAV” #3

-String in Python by Binit Jha 17


String Functions
count()

"dav".count(‘a’) #1
"dav bariatu".count('a’) #3
"dav bariatu".count('a',4,11) #2
"dav bariatu".count('a’,4) #2

-String in Python by Binit Jha 19


String Functions
index()

>>> "dav bariatu".index('a’) #1


>>> "dav bariatu".index('a',4,11)
#5

Note: Raises ValueError exception,


when not found the substring.
Here the limit 11 is exclusive.
-String in Python by Binit Jha 20
String Functions
2. find()

strn="python programming"
sub="prog“
strn.find(sub)
Output: 7
or
print(strn.find(sub,3,10))
Output: 1

Note: Returns -1, when not found the substring.


Here the limit 10 is exclusive.
-String in Python by Binit Jha 21
String Functions
3. is.alnum()
a="pass123“
print("isalnum(): ",a.isalnum())
Output: True

a=“ ”
print("isalnum(): ",a.isalnum())
Output: False

"pass 123“.alnum()
Output: False #due to space in between

-String in Python by Binit Jha 22


String Functions
4. isalpha() 5. isdigit()
Pass=“tata” Pass=“123”
Pass.isalpha()
Pass.isdigit()
o/p: True
Pass=“tata123” o/p: True
o/p: False Pass='a123b‘
Pass=“ “ o/p: False
o/p: False

"ta ta".isalpha() “1 23”.isdigit()


Output: False o/p: False
#due to space in between

-String in Python by Binit Jha 23


String Functions
6. islower() 7. isupper()
Pass=“dav” 8. lower()
Pass.islower() Pass=“DAV”
o/p: True Pass.lower()
Pass=“Dav” o/p: dav
o/p: False 9. upper()
Pass=“dav123” Pass=“dav”
o/p: True Pass.upper()
"dav 123".islower() o/p: DAV
o/p: T
-String in Python by Binit Jha 24
String Functions
10.isspace() lstrip()
11.
Name=“” Name= “
Name.isspace() IMPOSSIBLE”
o/p: False Name.lstrip()
Name=“ ” o/p: IMPOSSIBLE
o/p: True Name=
Name=“DAV Bariatu” “IMPOSSIBLE”
o/p: False Name.lstrip(‘IM’)
o/p: POSSIBLE
Name[11].isspace() Name.lstrip(‘MI’)
o/p: IndexError o/p: POSSIBLE
-String in Python by Binit Jha 25
String Functions
11 b.) rstrip() 11 c.) strip()

' DAV '.rstrip() ' DAV '.strip()


o/p: ' DAV’ o/p: 'DAV’

"DAV".rstrip("V") "VDAV".strip("V")
o/p: 'DA' o/p: 'DA'

-String in Python by Binit Jha 26


String Functions
12. startswith() 13. endswith()
"DAV".endswith("v")
o/p: False
"DAV".startswith("
"DAV".endswith("V")
D")
o/p: True
o/p: True "DAV ".endswith("V")
o/p: False
"DAV".startswith(" "DAV ".endswith("")
B") o/p: True
o/p: False "DAV ".endswith(" ")
o/p: True
-String in Python by Binit Jha 27
String Functions
14. replace()

"Rohit Sharma".replace('R','M’)
'Mohit Sharma'
"Rohit Rharma".replace('R','M’)
'Mohit Mharma'

-String in Python by Binit Jha 28


String Functions
15. join()

'*'.join("DAV")
'D*A*V'
' '.join("DAV")
‘D A V’
'*'.join(['Happy','vacation','to','all’])
'Happy*vacation*to*all’

NOTE: It works with string based iterator


(list or tuple consisting of strings)
-String in Python by Binit Jha 29
16. split() 17. partition()
"DAV BARIATU".partition('A’)
str="hello there,
how r u all?“
('D', 'A', 'V BARIATU’)
str.split(" ")

['hello', 'there,',
'how', 'r', 'u', 'all?’]
NOTE: the output will
be in tuples divided
NOTE: the o/p will into 3 parts.
be in list.

-String in Python by Binit Jha 30


Thank You!

You might also like