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

13 Python Programming

Uploaded by

Rudraaksh Sethi
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)
7 views

13 Python Programming

Uploaded by

Rudraaksh Sethi
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
You are on page 1/ 9

STRINGS-PART2

Session 13
String Methods
strip()-method returns a copy of the string with both leading and trailing
characters removed
E.g string = ‘ xoxo love xoxo '
# Leading whitepsace are removed
print(string.strip())
Answer – ‘xoxo love xoxo’
E.g string = 'android is awesome'
print(string.strip('an'))
Answer – driod is awesome (an removed from beginning)
lstrip()-method returns a copy of the string with leading characters removed (based on
the string argument passed).
E.g str = ‘ this is good '
# Leading whitepsace are removed
print(str.lstrip()) Answer – ‘this is good ‘
e.g website = 'https://www.programiz.com/'
print(website.lstrip('https://'))
Answer- ‘www.programiz.com’

rstrip() -method returns a copy of the string with trailing characters removed (based on
the string argument passed).
E.g str1 = ' this is good ‘
Str1.rstrip() returns
' this is good’
partition() -method splits the string at the first occurrence of the argument string and returns a
tuple containing the part the before separator, argument string and the part after the
separator.
E.g string = "Python is fun"
print(string.partition('is '))
Output -('Python ', 'is ', 'fun')
string.partition('not ')
Output-('Python is fun', ‘ ‘, ‘ ’) # 'not' separator is not found
join() - returns a string comprising elements of the sequence separated by the delimiter
E.g ‘ > ’,join( [ ‘ a ’ , ’ n ’ , ’ b ’] returns output ‘a>n>b ’
e.g numList = ['1', '2', '3', '4’] and seperator = ', ‘
print(seperator.join(numList))
output is ‘1, 2, 3, 4 ’
E.g s1 = 'abc’ s2 = '123’
s1.join(s2))
output - 1abc2abc3 """ Each character of s2 is concatenated to the front of s1"""
isspace()- -method returns True if there are only whitespace characters in the string. If
not, it return False.
E.g
s = '\t'
print(s.isspace())
-Answer True
s='a'
print(s.isspace())
Answer False
s = ''
print(s.isspace())
Answer False
isalpha()-method returns True if all characters in the string are alphabets.
If not, it returns False.
E.g 1 name = "Monica"
print(name.isalpha())-evaluates to True
Eg-2 name = "Monica Geller"
print(name.isalpha())
Answer -False(space is not aphabet)
# Eg -3
name = "Mo3nicaGell22er"
print(name.isalpha())
Answer -False contains digits
isdigit()-method returns True if all characters in a string are digits. If not, it returns False.
E.G
s = "28212"
print(s.isdigit()) -Answer –True
S=’12abx’
S.isdigit() -Answer- False

isalnum()-returns True if all characters in the string are alphanumeric (either alphabets or numbers).
If not, it returns False. E.g
name = "M234onica"
print(name.isalnum())
Answer -True
name = "M3onica Gell22er "
print(name.isalnum())
Answer -False # contains whitespace
startswith()-method returns True if a string starts with the specified prefix(string). If not, it
returns False.
text = "Python is easy to learn."
result = text.startswith('is easy')
# returns False
result = text.startswith('Python is ')
# returns True
endswith()- method returns True if a string ends with the specified suffix. If not, it returns False.
E.g text = "Python is easy to learn."
result = text.endswith('to learn')
print(result)
# returns False (as fullstop at end missing in string specified with endswith)
result = text.endswith('to learn.')
# returns True
encode()-returns string s in encoding form based on the given encoding

s.decode()- returns the decoded string s, based on the given encoding scheme
string = 'pythön!’
print('The string is:', string)
# default encoding to utf-8
s1= string.encode()
# print result
print('The encoded version is:',s1)

s2=s1.decode()
print("Decoded string is",s2)

Output
The string is: pythön!
The encoded version is:
b'pyth\xc3\xb6n!’
Decoded string is pythön!

You might also like