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

Class7 String Operantions Methods

Uploaded by

memu00133
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)
14 views24 pages

Class7 String Operantions Methods

Uploaded by

memu00133
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/ 24

Notebook

April 19, 2025

[ ]: # Immutable (Not Changable in acutual string variable)

[44]: a = "Wednesday"
"Sednesday"

[46]: a[6::] = "way"

---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
Cell In[46], line 1
----> 1 a[6::] = "way"

TypeError: 'str' object does not support item assignment

[49]: a[0] = "S"

---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
Cell In[49], line 1
----> 1 a[0] = "S"

TypeError: 'str' object does not support item assignment

[50]: # Mutable data type


l = [100 , 200 , 300 , 400 , 500]
l[0]

[50]: 100

[52]: l[0] = 9000

[53]: l

[53]: [9000, 200, 300, 400, 500]

1
1 String Operations
[ ]: # Indexing
# Slicing
# Concatination
# Identity
# Membership Operation

2 Concatination (+)
• joining two or more strings is concatination

[9]: a = "We are"


b = " Learning "
c = "Python"

[10]: a+b+c

[10]: 'We are Learning Python'

# Identity (is , is not)

[13]: a = "Wednesday"
b = "Wednesday"

[14]: print(id(a))
print(id(b))

1735102526960
1735102526960

[15]: a is b

[15]: True

[ ]: id(a) is id(b)

[16]: a is not b

[16]: False

[17]: str1= "Hello"


str2 = "Hello"

[18]: str1 is str2

[18]: True

[24]: addr_str1 = id(str1)

2
[27]: addr_str1

[27]: 1735150816880

[25]: addr_str2 = id(str2)

[26]: addr_str1 is addr_str2

[26]: False

3 Membership Operation (in , not in)


[28]: str1 = "Usman loves to play football"

[29]: str1

[29]: 'Usman loves to play football'

[30]: "football" in str1

[30]: True

[31]: "Cricket" in str1

[31]: False

[32]: "Cricket" not in str1

[32]: True

[33]: "l" in str1

[33]: True

[34]: "F" in str1

[34]: False

4 string methods
[ ]: Methods are functions that are defined in a class

[35]: a

[35]: 'Wednesday'

[37]: print(type(a))

3
<class 'str'>

[39]: print(dir(str))

['__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']

[40]: # capitalize()
s = "this is python"

[41]: s

[41]: 'this is python'

[42]: s.capitalize()

[42]: 'This is python'

[43]: s

[43]: 'this is python'

[ ]: a

[ ]: # upper()

[54]: s = "this is python"

[58]: s_upper_case = s.upper()

[59]: s_upper_case

[59]: 'THIS IS PYTHON'

[ ]: # lower()

[60]: s = 'THIS IS PYTHON'

4
[61]: s.lower()

[61]: 'this is python'

[ ]: # swapcase()
lower to upper
upper to lower

[62]: "apple BANANA".swapcase()

[62]: 'APPLE banana'

[ ]: # The methods which return bool values

[ ]: startswith()

[63]: s = "Python is a programming language"

[64]: s.startswith("P")

[64]: True

[65]: s.startswith("Z")

[65]: False

[66]: s.startswith("Python")

[66]: True

[ ]: # endswith()

[67]: s.endswith("Z")

[67]: False

[68]: s.endswith("e")

[68]: True

[69]: s.endswith("guage")

[69]: True

[ ]: isalpha
isnumeric
isalnum

5
[70]: # isalpha - True for alphabets and false for other char
s1 = "abc" # True
s2 = "123abc" # False
s3 = "$$$$" # False
s4 = "121323" # False
print(s1.isalpha())
print(s2.isalpha())
print(s3.isalpha())
print(s4.isalpha())

True
False
False
False

[71]: # isnumeric - True for numbers and false for other char
s1 = "abc" # false
s2 = "123abc" # False
s3 = "$$$$" # False
s4 = "121323" # True
print(s1.isnumeric())
print(s2.isnumeric())
print(s3.isnumeric())
print(s4.isnumeric())

False
False
False
True

[76]: # isalnum - True for if the string contains alphabet or number false otherwise
s1 = "abc" # True
s2 = "123abc" # True
s3 = "$$$$" # False
s4 = "121323" # True
print(s1.isalnum())
print(s2.isalnum())
print(s3.isalnum())
print(s4.isalnum())
s5 = "�����"
print(s5.isalnum())

True
True
False
True
False

6
[75]: # isascii
# isalnum - True for if the string is having an ASCII value false otherwise
s1 = "abc" # True
s2 = "123abc" # True
s3 = "$$$$" # True
s4 = "121323" # True
s5 = "�����" # False
print(s1.isascii())
print(s2.isascii())
print(s3.isascii())
print(s4.isascii())
print(s5.isascii())

True
True
True
True
False

[77]: # islower()
s = "hello"
s.islower()

[77]: True

[78]: s = "Hello"
s.islower()

[78]: False

[ ]: # isupper()

[79]: s = "hello"
s.isupper()

[79]: False

[80]: s = "HELLO"
s.isupper()

[80]: True

[ ]: strip
rstrip
lstrip

[ ]: strip()

7
[81]: s = " Welcome "

[82]: s

[82]: ' Welcome '

[83]: s.strip()

[83]: 'Welcome'

[84]: s = "************Welcome**************"
s.strip("*")

[84]: 'Welcome'

[85]: # rstrip()
s = " Welcome "
s.rstrip()

[85]: ' Welcome'

[86]: s = "************Welcome**************"
s.rstrip("*")

[86]: '************Welcome'

[87]: # lstrip()
s = " Welcome "
s.lstrip()

[87]: 'Welcome '

[88]: s = "************Welcome**************"
s.lstrip("*")

[88]: 'Welcome**************'

[89]: input("Enter the Password: ")

Enter the Password: Password

[89]: ' Password'

[95]: "Welcome".strip("Wle")

[95]: 'com'

[ ]: find
rfind

8
index
rindex

[ ]: # find() - it give the index number of first occurance of the specified char or␣
↪group of char from L to R

[100]: s = "Usman c loves to play chess"


s.find("c")

[100]: 6

[ ]: # rfind - it give the index number of first occurance of the specified char or␣
↪group of char from R to L

NOTE - it only gives positive index number only

[101]: s = "Usman c loves to play chess"


s.rfind("c")

[101]: 22

[102]: s = "nUsman"
s.find("n")

[102]: 0

[104]: s = "nUsnman"
s.rfind("n")

[104]: 6

[ ]: # index() - it give the index number of first occurance of the specified char␣
↪or group of char from L to R

[105]: s = "Usman c loves to play chess"


s.index("c")

[105]: 6

[ ]: # rindex - it give the index number of first occurance of the specified char or␣
↪group of char from R to L

NOTE - it only gives positive index number only

[106]: s = "nUsnman"
s.rindex("n")

[106]: 6

[ ]: # difference between find() and index()

9
[107]: s = "Football"

[108]: s.find("Z")

[108]: -1

[111]: s.index("Z")

---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
Cell In[111], line 1
----> 1 s.index("Z")

ValueError: substring not found

[ ]: ljust - leftadjustment
rjust - rightadjustment

[ ]: Indian

[112]: a = "Indian"

[113]: a.ljust(30)

[113]: 'Indian '

[ ]: 30
6
24

[114]: a.ljust(30 , "*")

[114]: 'Indian************************'

[115]: a.rjust(30)

[115]: ' Indian'

[ ]: split()
join()

[ ]: split() ---> converts a sting to a list


NOTE - delimiter by default is " "

[116]: s = "Usman loves to play chess"

[117]: s

10
[117]: 'Usman loves to play chess'

[118]: s.split()

[118]: ['Usman', 'loves', 'to', 'play', 'chess']

[120]: s = "Usman@loves@to@play@chess"
s.split("@")

[120]: ['Usman', 'loves', 'to', 'play', 'chess']

[121]: l = s.split("@")
l

[121]: ['Usman', 'loves', 'to', 'play', 'chess']

[ ]: join() - convert a list to a string

[123]: " ".join(l)

[123]: 'Usman loves to play chess'

[124]: "@".join(l)

[124]: 'Usman@loves@to@play@chess'

[129]: print(list(s))

['U', 's', 'm', 'a', 'n', '@', 'l', 'o', 'v', 'e', 's', '@', 't', 'o', '@', 'p',
'l', 'a', 'y', '@', 'c', 'h', 'e', 's', 's']

[ ]: replace
count

[ ]: # replace - it replaces old char to new char

[130]: s = "Usman loves to play chess"


s.replace("chess" , "football")

[130]: 'Usman loves to play football'

[132]: new_str = s.replace("chess" , "football")


new_str

[132]: 'Usman loves to play football'

[ ]: count()

[135]: new_str

11
[135]: 'Usman loves to play football'

[138]: new_str.count("o")

[138]: 4

[139]: a = "hello" # Immutable


b = [500 , 600 ,300] # mutable

[140]: a[0]

[140]: 'h'

[141]: a[0] = "D"

---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
Cell In[141], line 1
----> 1 a[0] = "D"

TypeError: 'str' object does not support item assignment

[142]: b[0]

[142]: 500

[143]: b[0] = -1000

[144]: b

[144]: [-1000, 600, 300]

[146]: s = "Usman@loves@,to@play@chess"
s.split("@,")

[146]: ['Usman@loves', 'to@play@chess']

[147]: s="I love to play football"


s.replace("I love to play football", "I love to play chess")

[147]: 'I love to play chess'

[148]: s

[148]: 'I love to play football'

12
[149]: s = "Usman loves to play chess"
s.split()

[149]: ['Usman', 'loves', 'to', 'play', 'chess']

[150]: l = ['Usman', 'loves', 'to', 'play', 'chess']

[151]: l

[151]: ['Usman', 'loves', 'to', 'play', 'chess']

[152]: " ".join(l)

[152]: 'Usman loves to play chess'

[153]: a = "lionel messi"

[154]: a[1: :-1]

[154]: 'il'

[ ]: i

[155]: a[1: :1]

[155]: 'ionel messi'

[ ]:

[ ]:

[ ]:

[ ]:

[ ]:

[ ]:

[ ]:

[ ]:

[ ]:

[ ]:

[ ]:

13
[ ]:

[ ]:

[ ]:

[ ]:

[ ]:

[ ]:

[ ]:

[ ]:

[ ]:

[ ]:

[ ]:

[ ]:

[ ]:

[ ]:

[ ]:

[ ]:

[ ]:

[ ]:

[ ]:

[ ]:

[ ]:

[ ]:

[ ]:

[ ]:

14
[ ]:

[ ]:

[ ]:

[ ]:

[ ]:

[ ]:

[ ]:

[ ]:

[ ]:

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