1

I am taking a course in basics to python and I am stuck on this lab where we have to take the input a full name:

ex Pat Silly Doe

and print it as

Doe, P.S.

or in the case they did not input a middle name it would just read: Doe, P.

We are only on slicing and formatting strings no if else yet but I feel I am overthinking this

I know I need to slice the input and I can do that by:

name = input()
splitname = name.split(' ')
print(splitname)

and that will split each part of the user inputs name up by the whitespaces entered which will then let me know if its 2 names or all 3. But I am kind of lost now as to what to do next. Now that I have the name split up how do I without printing it take each name and format it.

I'm not trying to get a direct answer to the lab I don't want to just copy paste code in to do it, I just want a little bump in the right direction.

quamrana
  • 37,849
  • 12
  • 53
  • 71
Mitchell
  • 15
  • 5
  • So, given your `splitname` can you print the last name? You will need to print `"Doe"` first as in your example data. – quamrana Jan 14 '21 at 16:48
  • how do I identify the split name so i can make it like lastname = splitname[3] ? I am doing 8 week courses and its just a lot at once that i feel im overthinking something simple.. – Mitchell Jan 14 '21 at 16:51
  • Look at the output of `splitname[-1]` and `splitname[:-1]` – Chris Jan 14 '21 at 16:52
  • So I see you know about using `[]`. There is python notation to index the last element. – quamrana Jan 14 '21 at 16:52
  • so -1 gives me just the last name and :-1 is everything before it! right so now that i can take out just the last name i can format the remaining first and middle name to just the first index so would that be {0:0} ? meaning only getting the 1st letter and stopping? – Mitchell Jan 14 '21 at 16:57
  • ah so firstname[0] but what if there is no middle name if i use 0 for first name and -1 for last name i cant use 1 for middle name or itll print last name 2 times because it is 1 and -1. – Mitchell Jan 14 '21 at 17:04
  • You don't really need to know how many names there are. Don't use `names[0]` but `names[:-1]` which means all but the last. Then you're fine whether it's one or two, you don't care – Tomerikoo Jan 14 '21 at 17:08

4 Answers4

1

Assuming that the last string is the surname (and so the part that must be held in full) you can do like this:

  1. split the string on "spaces"
  2. take the surname (last element)
  3. loop on list and take the first char
  4. print the final string

In python:

name_surname = "Pat Silly Doe"
names_list = name_surname.split(" ")
surname = names_list.pop(-1)

final_name = "{}, ".format(surname)

for name in names_list:
    if name.strip() != "":
        final_name += "{}.".format(name[0])

print(final_name)        
 
Giordano
  • 5,422
  • 3
  • 33
  • 49
1

You don't really have to know how many names there are, you can call the split() function which returns a list, and then keep the string at 'len(splitname)-1', then only take the first letter of the strings on the other indexes. You can try to code that yourself by now, but here is the code if you want it: first you get the first letter of the first and middle name(s):

firstLetters = [name[0] for index, name in enumerate(splitname) if index != len(splitname)-1]
joined_nameLetters = ' '.join([str(v) for v in firstLetters])

then you print the name you need:

print(splitname[len(splitname)-1]+', '+joined_nameLetters)
Gea
  • 19
  • 2
0

So you need to print the last name first, and then just the first letter of the other names:

name = input()
splitname = name.split(' ')
#print(splitname)
print(f'{splitname[-1]}, ' + '.'.join([n[0] for n in splitname[:-1]]) + '.')
quamrana
  • 37,849
  • 12
  • 53
  • 71
0

You can use the following code to split the name

name = input()
splitname = name.split(', ')
print(splitname)
print(splitname[0])
firstname = splitname[0].split(' ')
print(firstname)

splitname[1] is the lastname

firstname[0], firstname[1], ... is the firstname

Samuel
  • 228
  • 2
  • 13