Vishnu
Vishnu

Reputation: 11

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

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!

Upvotes: 0

Views: 1116

Answers (2)

Adon Bilivit
Adon Bilivit

Reputation: 27196

You could do it like this:

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

Output:

V.I.P

Upvotes: 3

khan
khan

Reputation: 168

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

Upvotes: 0

Related Questions