How to print dot(".") separated acronyms from a given string

1k Views Asked by At

help me with this solution: I have to print dot-separated acronyms. For example "Very Important Person"= V.I.P The code that I wrote is as follows:

string=input()
str_list=string.split()
acronym=""
for word in str_list:
    acronym+= word[0]+"."
print(acronym.upper())

The expected output is for "Very Important Person"= V.I.P, but I am getting V.I.P. So how can I stop python after it puts two dots? Any help will be much appreciated!

2

There are 2 best solutions below

0
SIGHUP On

You could do it like this:

s = "Very Important Person"
print('.'.join(c[0] for c in s.split()))

Output:

V.I.P
0
khan On

The code that you wrote adds a dot after each letter of the acronym. You can simply remove the last dot using string slicing.

string=input()
str_list=string.split()
acronym=""
for word in str_list:
    acronym+= word[0]+"."
# remove last character
acronym=acronym[:-1]
print(acronym.upper())