-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinitials_extract.py
34 lines (30 loc) · 1.35 KB
/
initials_extract.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
# Extract every first initials from given strings or list of string.
def initials_extract(phrase):
# check if given prhase is of type string.
if type(phrase) is str:
#print('Extracted from string')
words = phrase.split()
result = ""
for word in words:
result += ''.join(word[0])
stand_for = str(phrase)
return result.upper() + ' stand for ' + stand_for +'.'
# check if given phrase is of type list.
else:
#print('========== Extracted from list:==========')
for lst in phrase:
words = lst.split()
#print(lst)
#print(words)
result = ""
for word in words:
result += ' '.join(word[0])
stand_for = str(words).lstrip('[').rstrip(']').replace("'","").replace(",","").capitalize()
print(result.upper(),'stand for', stand_for + '.')
#print()
return 'Done!'
print(initials_extract("Universal Serial Bus")) # Should be: USB
print(initials_extract("local area network")) # Should be: LAN
print(initials_extract("Operating system")) # Should be: OS
print(initials_extract(['Red hat linux','ubuntu linux','debian linux','linux mint',
'fedpra linux','SuSe linux','centos linux','slackware linux']))