For school I have to write a program that requests a 3 part name and breaks it into a first, middle, and last name. I wrote the code but the alignment of my output is not right. I was wonderng if someone can tell me what I did wrong and how to fix it. Any help would be appreiated. Thanks!

def main():
name=input("Please enter a full name with 3 names, separated by one space: ")
space=name.find(" ")
first=name[:space]
middle=name[space+1:]
secondname=middle[:space]
last=middle[space:]
print(first)
print(secondname)
print(last)

main()

Output:
Please enter a full name with 3 names, separated by one space: francis ford coppola
francis
ford co
ppola

Please enter a full name with 3 names, separated by one space: billy dee williams
billy
dee w
illiams

Recommended Answers

All 7 Replies

Hi use code tag next time,mark your code and press "code"
You only find first space at index 7.
The second one is at index 12

>>> name = 'francis ford coppola'
>>> space=name.find(" ")
>>> space
7

This:

name = 'francis ford coppola'
l = []
found = name.find(" ")
while found > -1:
    l.append(found)
    found = name.find(" ", found + 1)

print(l) #[7, 12]

Or simpler.

>>> import re
>>> name = 'francis ford coppola'
>>> [m.start() for m in re.finditer(' ', name)]
[7, 12]

Then you can use it like this.

>>> name = 'francis ford coppola'
>>> space = [m.start() for m in re.finditer(' ', name)]
>>> space
[7, 12]
>>> name[:space[0]]
'francis'
>>> name[space[0]+1:space[1]]
'ford'
>>> name[space[1]+1:]
'coppola'

There is a simpler way,i guess this is to learn about string slice.
Look at this.

>>> name = 'francis ford coppola'
>>> first, middle, last = name.split()
>>> first
'francis'
>>> middle
'ford'
>>> last
'coppola'

I'm just starting out with Python. Some of those codes you posted I have not learned yet. The teacher wants us to post the first middle and last names on 3 separates lines using the whitespaces as the indexes.

def main():
    name=input("Please enter a full name with 3 names, separated by one space: ")
    space=name.find(" ")
    first=name[:space[0]]
    middle=name[space[0]+1:space[1]]
    last=name[space[1]+1:]
    print(first)
    print(middle)
    print(last)

main()

Error message:
Please enter a full name with 3 names, separated by one space: amanda robyn linden
Traceback (most recent call last):
File "C:/Documents and Settings/HP_Owner/Desktop/ass4a.py", line 11, in <module>
main()
File "C:/Documents and Settings/HP_Owner/Desktop/ass4a.py", line 4, in main
first=name[:space[0]]
TypeError: 'int' object is not subscriptable

import re

def main():
    name = 'francis ford coppola'
    #space = name.find(" ") #will only find first space at 7
    space = [m.start() for m in re.finditer(' ', name)] #[7, 12]
    first = name[:space[0]]
    middle = name[space[0]+1:space[1]]
    last = name[space[1]+1:]
    print(first)
    print(middle)
    print(last)

main()

Without using regex module.

def find_index(name):
    name_index = []
    found = name.find(" ")
    while found > -1:
        name_index.append(found)
        found = name.find(" ", found + 1)
    return name_index

def main():
    name = input("Please enter a full name with 3 names, separated by one space: ")
    #name = 'francis ford coppola'
    space = find_index(name)
    first=name[:space[0]]
    middle=name[space[0]+1:space[1]]
    last=name[space[1]+1:]
    print(first)
    print(middle)
    print(last)

main()

I really appreciate your help. Thanks for taking the time out of your day but alot of the stuff you are posting is too advanced for me. I'm only 1 month into learning Python and the teacher hasn't covered that stuff yet. He gave us a sample program and told us to expand upon it using the .find function to find the spaces in between the names. Here's the sample:

def main():
name=input("Please enter a first, middle and last name: ")
space=name.find(" ")
first=name[:space]
last=name[space+1:]
print(first)
print(last)

Somehow I have to include a middle name and put it on a line by itself as well...

Some of those codes you posted I have not learned yet. The teacher wants us to post the first middle and last names on 3 separates lines using the whitespaces as the indexes

You did not state any limitations in your question, so how are we to know what you do and do not know, and it is unreasonable to expect others to come up with solution after solution until you find one you like.

name = 'francis ford coppola'
first_name=""
middle_name=""
this_name=""
ctr=0
for ltr in range(len(name)):
    print "testing letter", name[ltr]
    if name[ltr]== " ":
        if ctr==0:
            first_name=this_name
            print "     ", this_name, "to first_name"
        elif ctr==1:
            middle_name=this_name
            print "     ", this_name, "to middle_name"
        ctr += 1
        this_name=""
    else:
        this_name += name[ltr]
        print "this_name is now", this_name
last_name=this_name
print "-"*30
print "first_name", first_name
print "middle_name", middle_name
print "last_name", last_name

From your original code

name = 'francis ford coppola'
space=name.find(" ")
first=name[:space]
 
remaining_name=name[space+1:]     ## skip the space
space=remaining_name.find(" ")
middle=remaining_name[:space]
last=remaining_name[space+1:]
 
print(first)
print(middle)
print(last)

Thank you so much for your help!!!! That's exactly what I was looking for. Sorry if my posts weren't clear enough. I am new to Python and new to programming. I don't know much about it yet. I am also new in this forum. First time posting.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.