r/pythonhelp • • May 13 '22

HOMEWORK Remove middle name and Initials

This questions says that I need to make it so remove middle will remove the middle initial and return the name back to the main procedure and will print only the first and last name, also to account for if the person puts a period or not in between the middle name and the last name. and wants to take the full name and give its Initials, also both need to be capitalized.

Edit: I added some more code but now I am getting a unsupported operand types for -: 'str' and 'int'

def Main():

fullname=input("Enter full name")

print(f'Name minus middle initial is {Remove_MiddleInitial(fullname)}')

print(f'Your initials are {Return_Initials(fullname)}')

def Remove_MiddleInitial(fullname):

space=" "

period="."

if period in fullname:

Newname=(fullname[:period-1]+fullname[period+1:])

else:

Newname=(fullname[:space+1]+fullname[space+3:])

def Return_Initials(fullname):

Main()

3 Upvotes

3 comments sorted by

1

u/htepO May 13 '22

There's really nothing in your code.

What are some example inputs and their respective outputs?

1

u/veecharony May 13 '22

input: john m.smith or john m smith (both should give same results)

output:

Name minus middle initial is John Smith

Your initials are JMS

1

u/htepO May 13 '22 edited May 13 '22

Using the builtin split(), capitalize(), upper(), and join() methods makes it a lot easier.

>>> fullname = "john m. smith"
>>> splitname = fullname.split()
['john', 'm.', 'smith']
>>> first_name = splitname[0].capitalize()
'John'
>>> last_name = splitname[2].capitalize()
'Smith'
>>> first_and_last = f"{first_name} {last_name}"
'John Smith'
>>> initial_list = [name[0].upper() for name in splitname]
['J', 'M', 'S']
>>> initials = ''.join(initial_list)
'JMS'