Home > Software engineering >  How to print dot(".") separated acronyms from a given string
How to print dot(".") separated acronyms from a given string

Time:02-28

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!

CodePudding user response:

You could do it like this:

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

Output:

V.I.P

CodePudding user response:

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())
  • Related