Home > Software engineering >  Printing the first name the first letter in the last name in Python from one user input [closed]
Printing the first name the first letter in the last name in Python from one user input [closed]

Time:09-30

I have a task to complete that requires me to write a function with only one parameter, that takes with an input form the user asking for their full name in a single string. The task is to print out the first name the first letter of the last name.

Example: "jeff haider" should be printed out as "jeffh"

I have tried with two parameters, and it worked fine

firstname   lastname [0]

but with one input and string its quite difficult.

Thanks in advance if some angels decide to answer

CodePudding user response:

def task(name):
     fullname = name.split()
     output = fullname[0]   fullname[-1][0]
     return output

In case you have someone who has more than 1 name.

CodePudding user response:

You can reuse your two parameter approach after str.splitting the single string you have:

firstname, lastname = name.split(maxsplit=1)
  • Related