-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathString-Questions.py
409 lines (369 loc) · 11.4 KB
/
String-Questions.py
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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
# Input a string and count number of times 'a', 'A' appear in the sentence.
s=input("Enter a string: ")
count=0
for i in range(0,len(s)):
if s[i]=='a' or s[i]=='A':
count+=1
print('Number of times \'a\' or \'A\' appear:',count)
# Input string and count number of vowels.
s=input("Enter a string: ")
count=0
for i in range(0,len(s)):
if s[i]=='a' or s[i]=='A' or s[i]=='e' or s[i]=='E' or s[i]=='i' or s[i]=='I' or s[i]=='o' or s[i]=='O' or s[i]=='u' or s[i]=='U':
count+=1
print('Number of times vowels appear:',count)
# Input string and count number of consonants.
s=input("Enter a string: ")
count=0
for i in range(0,len(s)):
if s[i]!='a' or s[i]!='A' or s[i]!='e' or s[i]!='E' or s[i]!='i' or s[i]!='I' or s[i]!='o' or s[i]!='O' or s[i]!='u' or s[i]!='U':
count+=1
print('Number of times consonants appear:',count)
# Input string and count number of characters.
s=input("Enter a string: ")
count=0
for i in range(0,len(s)):
count+=1
print('Number of characters appear:',count)
# Input string and count number of lowercase characters.
s=input("Enter a string: ")
count=0
for i in range(0,len(s)):
if s[i]=='a' or s[i]=='b' or s[i]=='c' or s[i]=='d' or s[i]=='e' or s[i]=='f' or s[i]=='g' or s[i]=='h' or s[i]=='i' or s[i]=='j' or s[i]=='k' or s[i]=='l' or s[i]=='m' or s[i]=='n' or s[i]=='o' or s[i]=='p' or s[i]=='q' or s[i]=='r' or s[i]=='s' or s[i]=='t' or s[i]=='u' or s[i]=='v' or s[i]=='w' or s[i]=='x' or s[i]=='y' or s[i]=='z':
count+=1
print('Number of characters appear:',count)
# Input string and count number of uppercase characters.
s=input("Enter a string: ")
count=0
for i in range(0,len(s)):
if s[i]=='A' or s[i]=='B' or s[i]=='C' or s[i]=='D' or s[i]=='E' or s[i]=='F' or s[i]=='G' or s[i]=='H' or s[i]=='I' or s[i]=='J' or s[i]=='K' or s[i]=='L' or s[i]=='M' or s[i]=='N' or s[i]=='O' or s[i]=='P' or s[i]=='Q' or s[i]=='R' or s[i]=='S' or s[i]=='T' or s[i]=='U' or s[i]=='V' or s[i]=='W' or s[i]=='X' or s[i]=='Y' or s[i]=='Z':
count+=1
print('Number of characters appear:',count)
# Check if 0 is in string and print number of times 0 appears
int=input("Enter a number: ")
str(int)
count=0
for i in range(len(int)):
if int[i]=='0':
count+=1
print('0' in int)
print("Number of times 0 appears:",count)
# Input string and check if palindrome.
s=input("Enter a string: ")
if s==s[::-1]:
print("It is a palindrome.")
else:
print("It is not a palindrome.")
# Input 2 strings, if 2nd string in 1st, print first 4 chars of 2nd string with 'REVERSE' as well
str1=input('Enter string: ')
str2=input('Enter string: ')
if str2 in str1:
str3=str2[0:4]+'REVERSE'
print(str3)
else:
print('')
# Input String and Count Number of Times a Appears
s=input('Enter a string: ')
count=0
for i in range(0,len(s)):
if s[i]=='a':
count+=1
print('Number of times \'a\' appear:',count)
# Print Patterns
str='abc'
# 1st Sub Part
for i in range(0,3):
for j in range(0,i+1):
print(str[i],end='')
print()
#2
for i in range(1,4):
for j in range(0,i):
print(str[j],end='')
print()
#3
for i in range(3,0,-1):
for j in range(0,i):
print(str[j],end='')
print()
#4
for i in range(-1,2,1):
for j in range(2,i,-1):
print(str[j],end='')
print()
#5
for i in range(1,4):
for r in range(1,i+1):
for j in range(0,i):
print(str[j],end='')
print()
# Number of Words in Sentence
s=input('Enter sentence: ')
count=0
for i in range(0,len(s)):
if s[i]==' ':
count+=1
print('Number of words in sentence:',(count+1))
# Check if word is in string
s=input('Enter string: ')
w=input('Enter word: ')
if w in s:
print('Word is in string.')
else:
print('Word is not in string.')
# Input n names and print Longest Name
n=int(input("Number of names: "))
s=input("Enter name: ")
max=s
for i in range(1,n):
s=input("Enter name: ")
while len(s)>len(max):
max=s
print('Longest name is',max)
# Input n names and print Shortest Name
n=int(input("Number of names: "))
s=input("Enter name: ")
min=s
for i in range(1,n):
s=input("Enter name: ")
while len(s)<len(min):
min=s
print('Shortest name is',min)
# Write a program to enter string and see if all characters are uppercase or lowercase
s=input("Enter a string: ")
if s.upper()==s:
print('All characters are uppercase.')
elif s.lower()==s:
print('All characters are lowercase.')
else:
print('All characters are neither lowercase nor uppercase.')
# Input string and count number of alphabets, numbers and special chars.
s=input("Enter a string: ")
counta,countn,counts=0,0,0
for i in range(0,len(s)):
if s[i]=='a' or s[i]=='b' or s[i]=='c' or s[i]=='d' or s[i]=='e' or s[i]=='f' or s[i]=='g' or s[i]=='h' or s[i]=='i' or s[i]=='j' or s[i]=='k' or s[i]=='l' or s[i]=='m' or s[i]=='n' or s[i]=='o' or s[i]=='p' or s[i]=='q' or s[i]=='r' or s[i]=='s' or s[i]=='t' or s[i]=='u' or s[i]=='v' or s[i]=='w' or s[i]=='x' or s[i]=='y' or s[i]=='z' or s[i]=='A' or s[i]=='B' or s[i]=='C' or s[i]=='D' or s[i]=='E' or s[i]=='F' or s[i]=='G' or s[i]=='H' or s[i]=='I' or s[i]=='J' or s[i]=='K' or s[i]=='L' or s[i]=='M' or s[i]=='N' or s[i]=='O' or s[i]=='P' or s[i]=='Q' or s[i]=='R' or s[i]=='S' or s[i]=='T' or s[i]=='U' or s[i]=='V' or s[i]=='W' or s[i]=='X' or s[i]=='Y' or s[i]=='Z':
counta+=1
elif '0'<=s[i]<='9':
countn+=1
else:
counts+=1
print('Number of alphabets:',counta)
print('Number of numbers:',countn)
print('Number of special characters:',counts)
# Input string and convert case of string.
n=input('Enter string: ')
if n==n.upper():
print(n.lower())
elif n==n.lower():
print(n.upper())
else:
print('Both upper and lower cases.')
# Input string and count number of letters, uppercase letters, lowercase letters, numbers and special characters.
s=input("Enter a string: ")
counta=countn=counts=countl=countu=0
for i in s:
if i.isalpha():
counta+=1
if i.islower():
countl+=1
if i.isupper():
countu+=1
if i.isnumeric():
countn+=1
if i.isalnum()==False and i.isspace()==False:
counts+=1
print('Number of lowercase characters:',countl)
print('Number of uppercase characters:',countu)
print('Number of alphabets:',counta)
print('Number of numbers:',countn)
print('Number of special characters:',counts)
# Input string and substring, print number of times substring occurs in string.
## Method 1
s=input('Enter string: ')
ss=input('Enter substring: ')
print('Number of times substring occurs in string:',s.count(ss))
## Method 2
s=input('Enter string: ')
ss=input('Enter substring: ')
count=0
if ss in s:
count+=1
print('Number of times substring occurs in string:',count)
# Input string containing decimal number and prints out decimal part of number.
s=input('Enter decimal number: ')
print('Decimal part of number: .'+str(s).split(".")[-1])
print("Decimal part of number: ."+s[s.index(".")+1:])
# Input string and capitalise first letter of each word
s=input('Enter string: ')
print('Capitalised string:',s.title())
# Input string, extract numbers and find sum
s=input('Enter string: ')
sum=0
for i in range(0,len(s)):
if '0'<=s[i]<='9':
sum+=int(s[i])
print('Sum of all numbers in string:',sum)
# Input string and print each word in new line
s=input('Enter string: ')
s=s.split(' ')
for word in s:
print(word)
# Input string, capitalise first letter of each word and create new string using that
s=input('Enter string: ')
snew=s.title()
print('Capitalised string:',snew)
# Check if word is in string
s=input('Enter string: ')
w=input('Enter word: ')
if w in s:
print('Word is in string.')
else:
print('Word is not in string.')
# Number of times word is in string
s=input('Enter string: ')
w=input('Enter word: ')
print('Number of times word in string:',s.count(w))
# Replace word in string with input word by user
s=input('Enter string: ')
w=input('Enter word for replacing: ')
wn=input('Enter word to replace: ')
s=s.replace(w,wn)
print('New string:',s)
# Input string and word, reverse word and make new text.
s=input('Enter string: ')
w=input('Enter word to replace: ')
wn=w[::-1]
s=s.replace(w,wn)
print('New string:',s)
# Input a string and check if it contains at least one digit or not.
## Method 1
n=input('Enter string: ')
count=0
for i in range(len(n)-1,-1,-1):
if n[i] in '0123456789':
count+=1
if count>=1:
if count==1:
print('String has one digit.')
elif count>1:
print('String has more than one digit.')
else:
print('String has no digit.')
## Method 2
s=input('Enter string: ')
count=0
for i in range(0,len(s)):
if '0'<=s[i]<='9':
count+=1
if count>=1:
if count==1:
print('String has one digit.')
elif count>1:
print('String has more than one digit.')
else:
print('String has no digit.')
## Method 3
s=input('Enter string: ')
for i in s:
if i.isnumeric():
n=True
else:
n=False
print(n)
# Input string then prints a string that capitalise every other letter in the string
s=input('Enter string: ')
size=True
ns=''
for i in range(0,len(s)):
if size==True:
ns+=s[i].capitalize()
size=False
else:
ns+=s[i].lower()
size=True
print('New string:',ns)
# Input number and convert it to roman numerals
n=int(input("Enter a number: "))
if n>3999:
print('Run program again and enter another number')
else:
v=[1000,900,500,400,100,90,50,40,10,9,5,4,1]
s=['M','CM','D','CD','D','XD','L','XL','X','IX','V','IV','I']
r=''
i=0
while n>0:
d=n//v[i]
n%=v[i]
while d:
r+=s[i]
d-=1
i+=1
print('Roman Numeral Form:',r)
# Convert binary number to decimal number
b=input('Enter binary number: ')
dp=b.find('.')
id,fd,twos=0,0,1
for i in range(dp-1,-1,-1):
id+=((ord(b[i])-ord('0'))*twos)
twos*=2
twos=2
for i in range(dp+1,len(b)):
fd+=((ord(b[i])-ord('0'))/twos)
twos*=2.0
print('Decimal converted number:',id+fd)
# Print toggle case for text given
s='Thursday Is Fun Day'
n=''
for i in range(0,len(s)):
if s[i].isupper():
n+=s[i].lower()
elif s[i].isalpha():
n+=s[i].upper()
else:
n+=' '
print(n)
# Input string and calculate number of characters, alphabets, numbers, special characters and words
s=input("Enter a string: ")
count=counta=countn=counts=countw=0
for i in s:
count+=1
if i.isalpha():
counta+=1
if i.isnumeric():
countn+=1
if i.isalnum()==False and i.isspace()==False:
counts+=1
for i in range(0,len(s)):
if s[i]==' ':
countw+=1
print('Number of characters:',count)
print('Number of alphabets:',counta)
print('Number of numbers:',countn)
print('Number of special characters:',counts)
print('Number of words:',countw+1)
# Input string and delete character
s=input('Enter string: ')
c=input('Enter character to be deleted: ')
sn=''
for i in s:
sn=s.replace(c,'')
print('New string without character:',sn)
# Input string and replace spaces with hyphens
s=input('Enter string: ')
sn=s.replace(' ','-')
print('New string without spaces:',sn)
# Input string and print longest word
s=input("Enter string: ")
s=s.split(' ')
maxword=''
for word in s:
while len(word)>len(maxword):
maxword=word
print('Longest substring is:',maxword)
# Input string and print each word in new line and capitalise each word
s=input('Enter string: ')
print('Capitalised string:',s.title())
s=s.split(' ')
print('Words present in string:')
for w in s:
print(w)