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([Link]())
Answer – ‘xoxo love xoxo’
E.g string = 'android is awesome'
print([Link]('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([Link]()) Answer – ‘this is good ‘
e.g website = '[Link]
print([Link]('[Link]
Answer- ‘[Link]’
rstrip() -method returns a copy of the string with trailing characters removed (based on
the string argument passed).
E.g str1 = ' this is good ‘
[Link]() 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([Link]('is '))
Output -('Python ', 'is ', 'fun')
[Link]('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([Link](numList))
output is ‘1, 2, 3, 4 ’
E.g s1 = 'abc’ s2 = '123’
[Link](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([Link]())
-Answer True
s='a'
print([Link]())
Answer False
s = ''
print([Link]())
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([Link]())-evaluates to True
Eg-2 name = "Monica Geller"
print([Link]())
Answer -False(space is not aphabet)
# Eg -3
name = "Mo3nicaGell22er"
print([Link]())
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([Link]()) -Answer –True
S=’12abx’
[Link]() -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([Link]())
Answer -True
name = "M3onica Gell22er "
print([Link]())
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 = [Link]('is easy')
# returns False
result = [Link]('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 = [Link]('to learn')
print(result)
# returns False (as fullstop at end missing in string specified with endswith)
result = [Link]('to learn.')
# returns True
encode()-returns string s in encoding form based on the given encoding
[Link]()- 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= [Link]()
# print result
print('The encoded version is:',s1)
s2=[Link]()
print("Decoded string is",s2)
Output
The string is: pythön!
The encoded version is:
b'pyth\xc3\xb6n!’
Decoded string is pythön!