-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstringg.py
More file actions
317 lines (262 loc) · 6.95 KB
/
stringg.py
File metadata and controls
317 lines (262 loc) · 6.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
# string datatype
"""
a = "my string"
b = 'your string'
c = "c"
print(c, type(c))
length_of_a = len(a)
print("Length of a:", length_of_a)
"""
# concat
"""
print(a + " " + b)
"""
# string: Ordered & Immutable collection of characters
# d = "Marry Christmas to all of you and a very very HAPPY NEW YEAR!"
# index: 0123456789..................................................60
# -ve: ....................................................987654321
# print(len(d))
# Accessing through index
"""
print(d[8])
print(d[-8])
print(d[0])
"""
# slicing
"""
print(d[2 : 9])
print(d[6 : 60 : 2])
print(d[10 : ])
print(d[ : 50])
print(d[ : ])
print(d[10 : 50 : ])
print(d[ : : ])
print(d[ : : 3])
print(d[-59 : -52])
print(d[-10 : 10 : -1])
print(d[4 : -3])
print(d[ : : -1])
print(d[ : : -3])
print(d[ : : 0])
"""
# functions vs. methods
d = "Hello friends chay pilo"
"""
print(d)
type(d)
len(d)
# methods:
# Case-related methods
# capitalize(string) wrong
e = d.capitalize()
print(e)
# print(d.upper())
# print(d.lower())
print(d.title())
print(d.swapcase())
print(d.casefold())
"""
# allignment related methods
"""
print(d.center(100))
print(d.center(100, "-"))
print(d.rjust(100))
print(d.rjust(100, "$"))
print(d.ljust(100))
print(d.ljust(100, "."))
"""
# count
"""
print(d.count('a'))
print(d.count('a', 6))
print(d.count('a', 6, 20))
print(d.count('very'))
print(d.count('very', 35))
print(d.count('very', 35, 42))
"""
# print(d.encode('utf-16'))
# Next Class: methods starting from endswith
#s1 = "Happy New Year! Wishing everyone of you a great 2023 ahead!"
"""
print(s1.endswith('!'))
print(s1.endswith('head!'))
print(s1.endswith('head..'))
print(s1.endswith('ear!', 0, 15))
print(s1.endswith('ahead!', 58))
print(s1.startswith('H'))
print(s1.startswith('h'))
print(s1.startswith('Hap'))
print(s1.startswith('Wish', 16))
print(s1.startswith('Wish', 16, 50))
"""
"""
print("Subject\t".expandtabs(30) + "Test1\tTest2\tFinal")
print("Maths\t".expandtabs(30) + "23\t20\t77")
print("Machine Learning\t".expandtabs(30) + "22\t19\t91")
print("C\t".expandtabs(30) + "24\t18\t72")
print("Environment\t".expandtabs(30) + "23\t25\t90")
print("C++\t".expandtabs(30) + "17\t22\t80")
print("Artificial Intelligence I\t".expandtabs(30) + "20\t24\t88")
print("Java\t".expandtabs(30) + "21\t23\t82")
print("Python\t".expandtabs(30) + "25\t25\t95")
"""
"""
print(s1.find('Y'))
print(s1.find('e'))
print(s1.find('e', 8))
print(s1.find('e', 12, 40))
print(s1.find('ing', -40, -4))
print(s1.find('z'))
print(s1.find('year'))
print("------------------")
print(s1.index('Y'))
print(s1.index('e'))
print(s1.index('e', 8))
print(s1.index('e', 12, 40))
print(s1.index('ing', -40, -4))
# print(s1.index('z'))
print("------------------")
print(s1.rfind('Y'))
print(s1.rfind('e'))
print(s1.rfind('e', 8))
print(s1.rfind('e', 12, 40))
print(s1.rfind('ing', -40, -4))
print("------------------")
print(s1.rindex('Y'))
print(s1.rindex('e'))
print(s1.rindex('e', 8))
print(s1.rindex('e', 12, 40))
print(s1.rindex('ing', -40, -4))
"""
# Methods starting from 'is': these will always return True/False
# print(s1.isupper())
# print(s1.islower())
# # print(s1.istitle())
# s1 = "Happy New Year!\nWishing everyone of you a great 2023 ahead! ©Alakh Pandya2023"
# s2 = "RoyalTechnosoft"
# print(s1.isalpha())
# print(s2.isalpha())
# s3 = "9825782290"
# print(s3.isnumeric())
# s4 = "RoyalTechnosoft9825782290"
# print(s4.isalnum())
# print(s2.isalnum())
# print(s3.isalnum())
# print(s3.isnumeric())
# print(s1.isascii())
# s5 = "fortryiselseifwhileelif"
# print(s5.isidentifier())
# print(s1.isprintable())
# s6 = " \t\t \t \n \n \t "
# print(s6.isspace())
s1 = "Happy New Year! Wishing everyone of you a great 2023 ahead!"
# print(s1.split())
# print(s1.split('a'))
# print(s1.split('a', 3))
# print(s1.rsplit('a'))
# print(s1.rsplit('a', 3))
# print(s1.split('everyone'))
# split_list = ['May', 'this', 'year', 'brings', 'you', 'everything', 'you', 'want!']
# s2 = "_".join(split_list)
# s2 = "\t".join(split_list)
# print(s2)
# print(s1.partition('everyone'))
# print(s1.partition('e'))
# print(s1.rpartition('e'))
# s2 = " Happy New Year!! "
# print(s2)
# print("Length =", len(s2))
# s3 = s2.lstrip()
# print(s3)
# print("Length =", len(s3))
# s4 = s2.rstrip()
# print(s4)
# print("Length =", len(s4))
# s5 = s2.strip()
# print(s5)
# print("Length =", len(s5))
s6 = "$$$$$$$$$$$$$$$$$$$$$Happy$New$Year!$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$"
# # print(s6.lstrip('$'))
# # print(s6.rstrip('$'))
# print(s6.strip('$'))
"""
"""
# s1 = "Hello"
# s2 = "Python!"
# print(s1 + s2)
# s1.__add__(s2)
# s3 = 5
# s4 = 10
# print(s3 + s4)
# s3.__add__(s4)
# s1 = "Hello friends chay pilo"
# print(s6.replace('$','-',2))
# s2 = s1.splitlines()
# print("Enter your date of birth (dd/mm):")
# dd = input("Date(dd): ")
# mm = input("Month(mm): ")
# print(f"Your next birthday will come on {dd.zfill(2)}/{mm.zfill(2)}/2023")
# Next Class: Lists
# HW: find out the difference between .isnumeric(), .isdigit() & .isdecimal()
# string examples for homework:
"""
1. Write a program that takes your full name as input and displays the abbreviations of the first and middle names except the last name which is displayed as it is.
Example:
input: Alakh Janakkumar Pandya
output: A.J.Pandya
name = input("Enter Your Name:-")
n1 = name.title()
n2 = n1.split(" ")
f = n2[0]
n2[0] = f[:1:]
s = n2[1]
n2[1] = s[:1:]
n3 = ".".join(n2)
print(n3)
"""
"""
2. Write a program to find the number of vowels, consonents and white space characters in a given string.
Example:
input string: Python Programming
output:
vowels: 4
whitel spaces: 1
consonents: 13
str = input("Enter a string:-")
str1 = str.lower()
s1 = int(str1.count('a'))
s2 = int(str1.count('e'))
s3 = int(str1.count('i'))
s4 = int(str1.count('o'))
s5 = int(str1.count('u'))
v = s1+s2+s3+s4+s5
s = str1.count(" ")
c = len(str1) - (v+s)
print(f"Vowels :- {v}")
print(f"whitel spaces :- {s}")
print(f"consonents :- {c}")
"""
"""
3. Write a program to make a new string with the word "the" deleted in the given string.
eg:
input string: This is the lion in the cage.
output: This is lion in cage.
str = input("Enter your string:-")
print("Input String:",str)
str1 = str.split('the ')
str2 = "".join(str1)
print("Output:",str2)
"""
"""
4. Write a Python code that asks a string from user and replace the first occurance of " " with "_" and last occurance of " " with "#".
Example:
input string: Keep yourself mute while not speaking.
output: Keep_yourself mute while not#speaking.
str = input("Enter a string:-")
print("Input string:-",str)
str1 = str.split(" ",1)
str2 = "_".join(str1)
str3 = str2.rsplit(" ",1)
str4 = "#".join(str3)
print("Output:-",str4)
"""