Extract First name, Middle name and last name | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Extract First name, Middle name and last name

Problem Statement: You need to process an input string, accepted from standard input. These are basically names which are to be separated in First Name, Middle Name and Last Name. In the platform you need to process a single string from standard. But you can extend the code later in your system so that you read data from an excel file (consisting of names) and produce a csv file as output. Input: You need to read a line from STDIN to extract First Name, Middle Name and Last Name Output: First Name, Middle Name and Last Name (separated by a single comma) Test Cases: Input: Krunalkumar Haribhai Ramanujsadhu Output: Krunalkumar,Haribhai, Ramanujsadhu Input:Mukul Gupta Output:Mukul,,Gupta Input:Manoj Kumar Singh Gowda Output:Manoj,Kumar,Singh,Gowda My attempt with python https://code.sololearn.com/c1eKydNDawy7/?ref=app

30th Sep 2021, 4:06 AM
Manoj Kumar T
Manoj Kumar T - avatar
22 Answers
+ 9
https://code.sololearn.com/c3u7K9XXLQoe/?ref=app https://code.sololearn.com/c32exzR2P122/?ref=app
2nd Oct 2021, 12:23 AM
Pallavi Bhardwaj
Pallavi Bhardwaj - avatar
+ 8
Màñøj Check this code u will get that u want.... https://code.sololearn.com/c32exzR2P122/?ref=app
2nd Oct 2021, 3:04 AM
Pallavi Bhardwaj
Pallavi Bhardwaj - avatar
+ 7
Màñøj ok I will fix it soon
2nd Oct 2021, 3:00 AM
Pallavi Bhardwaj
Pallavi Bhardwaj - avatar
+ 4
variables must never start with numbers. your function expects 3 parameters and you only passed one. you are separating(split) using an empty string, this will separate Python is sensitive to indentation
30th Sep 2021, 4:21 AM
Erlénio.RS
Erlénio.RS - avatar
+ 4
Rules for creating variables in Python: A variable name must start with a letter or the underscore character. (Your fault ) A variable name cannot start with a number. A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9, and _ ).
30th Sep 2021, 4:24 AM
Alireza Abbasi
Alireza Abbasi - avatar
+ 3
There are some issues in your code: 1.variable names can't start with a number. 2.split() seperates words with spaces, lines, and so on, but split("") does nothing. 3. Thete is an extra space before the print function. 4. The function needs 3 variables, but you give it a list. Look at this: https://code.sololearn.com/c4046t6IgkIO/?ref=app
30th Sep 2021, 4:30 AM
Arsalan [Inactive]
Arsalan [Inactive] - avatar
+ 2
Màñøj def sep_names (name): return ("{}, {}, {}". format (name[0], name[1], name[2])) print (sep_names (input().split(' '))) or def sep_names (fname, mname, lame): return ("{}, {}, {}".format(fname, mname, lame)) print (sep_names(*input().split(' ')))
30th Sep 2021, 4:36 AM
Erlénio.RS
Erlénio.RS - avatar
+ 2
✌️cool
30th Sep 2021, 4:39 AM
Erlénio.RS
Erlénio.RS - avatar
+ 2
Màñøj Here's a way for you to look at; first, *middle, last = input().split() print(f"{first}, {middle[0] if middle else ''}, {last}") Using the packing operator * for the second variable name will place any remaining values (names) into a list by the name 'middle'. Then you can just output the 0th value if the list isn't None. The first and last names will always need to be supplied and their values will be applied to the corresponding names.
30th Sep 2021, 7:51 AM
ChaoticDawg
ChaoticDawg - avatar
+ 1
Yeah ur correct, thanks u for help to understand
30th Sep 2021, 4:37 AM
Manoj Kumar T
Manoj Kumar T - avatar
+ 1
What if we give 2 values instead of 3 values
30th Sep 2021, 4:44 AM
Manoj Kumar T
Manoj Kumar T - avatar
+ 1
Màñøj I changed the code. Take a look.
30th Sep 2021, 4:53 AM
Arsalan [Inactive]
Arsalan [Inactive] - avatar
+ 1
Nice work👍
30th Sep 2021, 4:54 AM
Manoj Kumar T
Manoj Kumar T - avatar
+ 1
Màñøj txt=input().split() if len(txt)!=3: print('invalid input, enter more than 3 word') else: sep=',' print(sep.join(txt))
30th Sep 2021, 6:22 AM
Hemmat
Hemmat - avatar
+ 1
Màñøj Well, it's easy. ( if) can be changed. important using if join method.
30th Sep 2021, 7:47 AM
Hemmat
Hemmat - avatar
+ 1
Try this 😊 : type=["First","Middle","Last"] for i,name in enumerate(input().split()): print(type[i]+" name is '"+name+"'") https://code.sololearn.com/crUNEMX185xX/?ref=app
1st Oct 2021, 8:45 AM
SAN
SAN - avatar
+ 1
print(','.join(input().split()))
1st Oct 2021, 2:55 PM
Ravi V
Ravi V - avatar
+ 1
This is your python coding :-💯 def sep_names (fname, mname, lame): return ("{}, {}, {}".format(fname, mname, lame)) print (sep_names(*input().split(' ')))
2nd Oct 2021, 2:22 AM
Shubham Bhatia
Shubham Bhatia - avatar
0
Hemmat Works only if we enter more than 3 values.. But it should work for all values
30th Sep 2021, 6:55 AM
Manoj Kumar T
Manoj Kumar T - avatar
0
Look at the testcases SAN
1st Oct 2021, 1:58 PM
Manoj Kumar T
Manoj Kumar T - avatar