# STRIN LSTRIP & RSTRIP
txt = " banana "x = txt.lstrip() y = txt.rstrip()print("of all fruits", x, "is my favorite")print("of all fruits", y, "is my favorite")# STRIN LSTRIP & RSTRIP
txt = " banana "x = txt.lstrip() y = txt.rstrip()print("of all fruits", x, "is my favorite")print("of all fruits", y, "is my favorite")of all fruits banana is my favorite
of all fruits banana is my favorite
#STRING INDEXtxt = "Hello, Hello to my world."x = txt.index("Hello")print(x) #If the value is not found, the find() method returns -1, but the index() method will raise an exception:txt = "Hello, welcome to my world."print(txt.find("q"))print(txt.index("q")) 0 -1--------------------------------------------------------------------------- ValueError Traceback (most recent call last) Cell In [26], line 12 9 txt = "Hello, welcome to my world." 11 print(txt.find("q")) ---> 12 print(txt.index("q")) ValueError: substring not found
#STRING FIND
txt = "Hello, welcome to my world."x = txt.find("welcome")print(x) txt = "Hello, welcome to my world."x = txt.find("mani")print(x) txt = "Hello, welcome to my world."x = txt.find("e", 5, 10)print(x) 7
-1
8
#STRING ENDS WITHtxt = "Hello, welcome to my world."x = txt.endswith(".")print(x) txt = "Hello, welcome to my world."x = txt.endswith("my world.")print(x) txt = "Hello, welcome to my world."x = txt.endswith("my world.", 5, 11)print(x) True
True
False
#ENCODE
txt = "My name is Ståle"x = txt.encode()print(x) txt = "My name is Ståle"print(txt.encode(encoding="ascii",errors="backslashreplace"))print(txt.encode(encoding="ascii",errors="ignore"))print(txt.encode(encoding="ascii",errors="namereplace"))print(txt.encode(encoding="ascii",errors="replace"))print(txt.encode(encoding="ascii",errors="xmlcharrefreplace")) b'My name is St\xc3\xa5le'
b'My name is St\\xe5le'
b'My name is Stle'
b'My name is St\\N{LATIN SMALL LETTER A WITH RING ABOVE}le'
b'My name is St?le'
b'My name is Ståle'
#STRING PADDING - CENTER
txt = "Buddy"x = txt.center(20, "O")print(x) #COUNTtxt = "I love apples, apple are my favorite fruit"x = txt.count("apple")print(x) ##SYNTAX##string.count(value, start, end) txt = "I love apples, apple are my favorite fruit"x = txt.count("apple", 10, 24)print(x) OOOOOOOBuddyOOOOOOOO
2
1
#ESCAPE CHARACTERS
txt = "We are the \"Buddies\" from the south."print("OUTPUT 1 : ",txt)txt = 'It\'s alright.'print("OUTPUT 2 : ",txt) txt = "This will insert one \\ (backslash)."print("OUTPUT 3 : ",txt) txt = "Hello\nBuddy!"print("OUTPUT 4 : ",txt) txt = "Hello\rBuddy!"print("OUTPUT 5 : ",txt) txt = "Hello\tBuddy!"print("OUTPUT 6 : ",txt) #THIS EXAMPLE ERASES ONE CHARACTER (BACKSPACE):txt = "Hello \bBuddy!"print("OUTPUT 7 : ",txt) OUTPUT 1 : We are the "Buddies" from the south.
OUTPUT 2 : It's alright.
OUTPUT 3 : This will insert one \ (backslash).
OUTPUT 4 : Hello
Buddy!
Buddy! 5 : Hello
OUTPUT 6 : Hello Buddy!
OUTPUT 7 : HelloBuddy!
What is Kiro Kiro is an innovative AI-powered IDE that revolutionizes software development through intelligent assistance and structured wor...