Python-Strings.
ipynb - Colab 06/08/24, 3:20 PM
1 S = 'Hello World'
2
3 for i in S:
4 print(i)
H
e
l
l
o
W
o
r
l
d
if 'Hello' in S:
1 S = 'Hello World'
print(S)
2
3 if 'Hello' in S:
4 print(S)
Hello World
1 else:
S = 'Hello World'
2 print("Not found")
3 if 'hello' in S:
4 print(S)
5 else:
6 print("Not found")
Not found
1 if
S =[Link]("!"):
"Good Morning, Sunshine!"
2 print("!")
if [Link]("Good"):
3 print("Good")
4
5 if [Link]("!"):
6 print("!")
Good
!
[Link] Page 1 of 15
[Link] - Colab 06/08/24, 3:20 PM
1 S = "Good Morning, Sunshine! Hope your day is Good, sunshine!"
2
3 print([Link]("Good"))
4 print([Link]("Sunshine"))
5 print([Link]("o"))
2
1
7
1 S = "Good Morning, Sunshine! Hope your day is Good, sunshine!"
2
3 print([Link]("your"))
4 print([Link]("Good"))
5 print([Link]("great"))
6 print([Link]("your", 0, 10))
7 print([Link]("Good", 5))
8 print([Link]("great", 2, 15))
29
0
-1
-1
41
-1
[Link] Page 2 of 15
[Link] - Colab 06/08/24, 3:20 PM
1 # Searching for all occurences of a substring
2 # in a string
3
4 string = input("Enter the string to search in:")
5 substring = input("Enter the substring to search for:")
6
7 index = -1
8 indices = []
9
10 while True:
11 index = [Link](substring, index+1)
12 if index == -1: break
13 [Link](index)
14
15 print(indices)
16 print(string)
17
18 col = 0
19 for i in indices:
20 print(' '*(i-col), end='')
21 print('^', end='')
22 col = i+1
23 print()
Enter the string to search in:This is a demo
Enter the substring to search for:is
[2, 5]
This is a demo
^ ^
[Link] Page 3 of 15
[Link] - Colab 06/08/24, 3:20 PM
1 # Searching for all occurences of a substring
2 # in a string in reverse order
3
4 string = input("Enter the string to search in:")
5 substring = input("Enter the substring to search for:")
6
7 index = len(string) - len(substring) + 1
8 indices = []
9
10 while True:
11 index = [Link](substring, 0, index+len(substring) - 1)
12 if index == -1: break
13 [Link](index)
14
15 print(indices)
16 print(string)
17 col = 0
18
19 for i in reversed(indices):
20 print(' '*(i-col), end='')
21 print('^', end='')
22 col = i+1
23 print()
Enter the string to search in:This is a demo
Enter the substring to search for:is
[5, 2]
This is a demo
^ ^
1 S = "India: New Delhi"
2
3 print([Link](":"))
('India', ':', ' New Delhi')
1 S = "India: New Delhi, China: Beijing"
2
3 print([Link](":"))
('India', ':', ' New Delhi, China: Beijing')
[Link] Page 4 of 15
[Link] - Colab 06/08/24, 3:20 PM
1 S = "India: New Delhi, China: Beijing"
2
3 print([Link](":"))
('India: New Delhi, China', ':', ' Beijing')
1 S = "India: New Delhi"
2
3 print([Link]("-"))
('India: New Delhi', '', '')
1 S = "India: New Delhi"
2
3 print([Link]("-"))
('', '', 'India: New Delhi')
1 S = "India: New Delhi"
2
3 print([Link]())
['India:', 'New', 'Delhi']
1 S = "India: New Delhi, China: Beijing"
2
3 print([Link]())
['India:', 'New', 'Delhi,', 'China:', 'Beijing']
1 S = " India: New Delhi, China: Beijing "
2
3 print([Link]())
['India:', 'New', 'Delhi,', 'China:', 'Beijing']
1 S = "India: New Delhi"
2
3 print([Link](":"))
['India', ' New Delhi']
[Link] Page 5 of 15
[Link] - Colab 06/08/24, 3:20 PM
1 S = "India: New Delhi, China: Beijing"
2
3 print([Link](":"))
['India', ' New Delhi, China', ' Beijing']
1 S = " \t\nIndia:New \n\r Delhi\t\t\tChina:Beijing\n\n"
2
3 [Link]()
['India:New', 'Delhi', 'China:Beijing']
1 S = ""
2
3 [Link]()
[]
1 S = " "
2
3 [Link]()
[]
1 S = "5399269985"
2 [Link]("99")
['53', '26', '85']
1 S = "5399269985"
2 [Link]("99", 1)
['53', '269985']
1 S = "5399269985"
2 [Link]("99", 2)
['53', '26', '85']
1 S = "5399269985"
2 [Link]("99", 3)
['53', '26', '85']
[Link] Page 6 of 15
[Link] - Colab 06/08/24, 3:20 PM
1 S = "a b c d e f"
2 [Link]()
['a', 'b', 'c', 'd', 'e', 'f']
1 S = "a b c d e f"
2 [Link](None, 1)
['a', 'b c d e f']
1 S = "a b c d e f"
2 [Link](None, 2)
['a', 'b', 'c d e f']
1 S = "a b c d e f"
2 [Link](None, 4)
['a', 'b', 'c', 'd', 'e f']
1 S = "a b c d e f"
2 [Link](None, -1)
['a', 'b', 'c', 'd', 'e', 'f']
1 S = "125,342,264"
2 [Link]()
['125,342,264']
1 S = "125,342,264"
2 [Link](',')
['125', '342', '264']
1 S = "125,342,264"
2 [Link](None,1)
['125,342,264']
[Link] Page 7 of 15
[Link] - Colab 06/08/24, 3:20 PM
1 S = "125,342,264"
2 [Link](',',1)
['125', '342,264']
1 S = "125,342,264"
2 [Link](None,-1)
['125,342,264']
1 S = "125,342,264"
2 [Link](',',-1)
['125', '342', '264']
1 S = "Hello\nWorld\nHow are you?"
2
3 [Link]()
['Hello', 'World', 'How are you?']
1 S = "Hello\nWorld\nHow are you?"
2
3 [Link](True)
['Hello\n', 'World\n', 'How are you?']
1 S = "Hello\nWorld\nHow are you?"
2
3 [Link](False)
['Hello', 'World', 'How are you?']
1 " Hello world ".lstrip()
'Hello world '
1 "\t\r\tHello world ".lstrip()
'Hello world '
[Link] Page 8 of 15
[Link] - Colab 06/08/24, 3:20 PM
1 " 123\r\tHello world ".lstrip()
'123\r\tHello world '
1 " 1234Hello world ".lstrip('019876')
' 1234Hello world '
1 "1234Hello world ".lstrip('019876')
'234Hello world '
1 "1234Hello world ".lstrip('0129876')
'34Hello world '
1 "126834Hello world ".lstrip('0129876')
'34Hello world '
1 "126834Hello world ".rstrip()
'126834Hello world'
1 "126834Hello world \n\t ".rstrip()
'126834Hello world'
1 "126834Hello world 126834".rstrip('0129876')
'126834Hello world 126834'
1 "126834Hello world 834126".rstrip('0129876')
'126834Hello world 834'
1 "126834Hello world 126".rstrip('0129876')
'126834Hello world '
[Link] Page 9 of 15
[Link] - Colab 06/08/24, 3:20 PM
1 " \n\t 126834Hello world \n\t ".strip()
'126834Hello world'
1 "126834Hello world 126".strip('0129876')
'34Hello world '
1 S = "Delhi-Bombay-Bangalore-Bombay"
2
3 [Link]("Bombay", "Mumbai")
'Delhi-Mumbai-Bangalore-Mumbai'
1 S = "Delhi-Bombay-Bangalore-Bombay"
2
3 [Link]("Bombay", "Mumbai", 1)
'Delhi-Mumbai-Bangalore-Bombay'
1 S = "Delhi-Bombay-Bangalore-Bombay"
2
3 [Link]("Bombay", "Mumbai", 3)
'Delhi-Mumbai-Bangalore-Mumbai'
1 from string import Template
2
3 t = Template("The sum of $x and $y is $z")
4
5 d = dict(x=10, y=20, z=30)
6
7 [Link](d)
'The sum of 10 and 20 is 30'
[Link] Page 10 of 15
[Link] - Colab 06/08/24, 3:20 PM
1 from string import Template
2
3 t = Template("The sum of $x and $y is $z")
4
5 d = {'x':10, 'y':20, 'z':30}
6
7 [Link](d)
'The sum of 10 and 20 is 30'
1 from string import Template
2
3 t = Template("The sum of $x and $y is $z")
4
5 [Link](dict(x=10, y=20, z=30))
'The sum of 10 and 20 is 30'
1 from string import Template
2
3 t = Template("The sum of $x and $y is $z")
4
5 [Link](dict(x=10, y=20, z=30, a=100))
'The sum of 10 and 20 is 30'
1 from string import Template
2
3 t = Template("The sum of $x and $y is $z")
4
5 [Link](dict(x=10, y=20, z=30), x=20,y=10)
'The sum of 20 and 10 is 30'
1 from string import Template
2
3 t = Template("The sum of $x and $y is $z")
4
5 [Link](dict(x=10, y=20, z=30), z=100)
'The sum of 10 and 20 is 100'
[Link] Page 11 of 15
[Link] - Colab 06/08/24, 3:20 PM
1 from string import Template
2
3 t = Template("The sum of $x and $y is $z")
4
5 [Link](dict(x=10, z=30))
--------------------------------------------------------------------------
-
KeyError Traceback (most recent call
last)
<ipython-input-73-17f5aa3ddc4f> in <cell line: 5>()
3 t = Template("The sum of $x and $y is $z")
4
----> 5 [Link](dict(x=10, z=30))
1 frames
/usr/lib/python3.10/[Link] in convert(mo)
112 named = [Link]('named') or [Link]('braced')
113 if named is not None:
--> 114 return str(mapping[named])
115 if [Link]('escaped') is not None:
116 return [Link]
KeyError: 'y'
1 from string import Template
2
3 t = Template("The sum of $x and $y is $z")
4
5 t.safe_substitute(dict(x=10, z=30))
'The sum of 10 and $y is 30'
1 d={'1':'a','2':'b','3':'c','4':'d','5':'e','6':'f','7':'g','8':None,'9':None}
2
3 t = [Link](d)
4
5 "hello 0468 world".translate(t)
'hello 0df world'
1 t = [Link]('aeiou', 'AEIOU')
2
3 "This is a demonstration.".translate(t)
'ThIs Is A dEmOnstrAtIOn.'
[Link] Page 12 of 15
[Link] - Colab 06/08/24, 3:20 PM
1 s = 'Hello'
2
3 [Link](7)
'Hello '
1 s = 'Hello'
2
3 [Link](7, "=")
'Hello=='
1 s = 'Hello'
2
3 [Link](7, "=")
'==Hello'
1 s = 'Hello'
2
3 [Link](7, "=")
'=Hello='
1 s = 'Hello'
2
3 [Link](7)
'00Hello'
1 s = '+Hello'
2
3 [Link](7)
'+0Hello'
1 s = '+123'
2
3 [Link](7)
'+000123'
[Link] Page 13 of 15
[Link] - Colab 06/08/24, 3:20 PM
1 '{}'.format(15)
'15'
1 '{:d}'.format(15)
'15'
1 '{:b}'.format(15)
'1111'
1 '{:x}'.format(15)
'f'
1 '{:X}'.format(15)
'F'
1 '{:n}'.format(15)
'15'
1 '{:c}'.format(65)
'A'
1 Start coding or generate with AI.
[Link] Page 14 of 15
[Link] - Colab 06/08/24, 3:20 PM
[Link] Page 15 of 15