Marcus Colin Harvison
Marcus Colin Harvison

Reputation: 13

How could you format a full name into just initials? (X.X.X.)

I'm trying to turn a full name into just the initials from one string. My logic is to capitalize all the names in the string, break the string by the spaces into a list, then select the first character of each index, then join the characters into a single string divided by periods.

I'm having trouble with this and not sure if there's a better way.

Here's my current progress:

def main():
    fstn= input("Enter your full name:")

    fstn=fstn.title()
    fstn= fstn.split(" ")
    for i in fstn:
        fstn= i[0]
        print(fstn)


main()

This prints out each initial on a different line, how would i finish this?

Upvotes: 1

Views: 737

Answers (2)

galaxyan
galaxyan

Reputation: 6141

def main():
    fstn= input("Enter your full name:")
    print ('.'.join(word[0] for word in fstn.split(" ")).upper()) #for python 3


main()

Upvotes: 2

Stack
Stack

Reputation: 4526

Hi check this example out,

def main():
    fstn= input("Enter your full name:")
    fstn=fstn.title()
    fstn= fstn.split(" ")
    out_str = ""
    for i in fstn:
        out_str = out_str + i[0] + "."
    print(out_str)

main()

Upvotes: 0

Related Questions