Question

Often times data is not always exactly in the format you need. For this exercise you...

Often times data is not always exactly in the format you need. For this exercise you will write a

function in Python that parses a string containing a name into its constituent parts: a first name and a last

name. What makes this exercise interesting is that the name string may be formatted in one of

two ways:

"Last, First"

"First Last"

To complete this exercise, you should create a function and comment appropriately. For technical

inspiration look at the in operator, split, and find string methods.

Homework Answers

Answer #1

Code:

Code as text:

def parseName(name):

''' this functions parses a name string into two strings of

first and last name.

Input: a string containing name formatted

Return: two strings containing first and last name

'''

# check if name has "," in it, if yes then split by ","

if name.find(',') != -1:

last, first = name.split(', ')

else: # else split by space " "

first, last = name.split(' ')

return first, last # return first and last names


# test the function

name = input("Enter name: ")

first, last = parseName(name)

print("The name is: ", first, last)

Sample run:

P.s. ask any doubts in comments and don't forget to rate the answer.

'1 HIT +5: def parseName (name) : this functions parses a name string into two strings of first and last name. Input: a string containing name formatted Return: two strings containing first and last name, # check if name has "," in it, if yes then split by "," if name.find(',') != -1: last, first = name.split(, '), else: # else split by space "" first, last = name.split(" "); return first, last # return first and last names # test the function print('Test 1') first, last = parseName("Jane Doe") # name space seperated print('First name: ', first) print('Last name: ', last) print('Test 2') first, last = parseName('Doe, Jane') # name comma seperated print('First name: ', first) print('Last name: ', last) 24 25 26

Test 1 First name: Jane Last name: Doe Test 2 First name: Jane Last name: Doe

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
PYTHON : Create a Email Address Parser (* Please do make comments*) Often times, you may...
PYTHON : Create a Email Address Parser (* Please do make comments*) Often times, you may be given a list of raw email addresses and be asked to generate meaningful information from such a list. This project involves parsing such a list and generating names and summary information from that list. The script, eparser.py, should: Open the file specified as the first argument to the script (see below) Read the file one line at a time (i.e., for line in...
Python (Using for reference please comment which is which!) Exercise 1: Store Unique Words as Dictionary...
Python (Using for reference please comment which is which!) Exercise 1: Store Unique Words as Dictionary Keys Write a program that stores unique words as keys in a dictionary. Don't worry about upper/lowercase for now (i.e., it's ok for you to have separate entries for "The" and "the"), and just assign 1 as the value for each key. Use an input loop (while loop with an input function) to accepts values putting them into the dictionary until the user enters...
I NEED TASK 3 ONLY TASK 1 country.py class Country:     def __init__(self, name, pop, area, continent):...
I NEED TASK 3 ONLY TASK 1 country.py class Country:     def __init__(self, name, pop, area, continent):         self.name = name         self.pop = pop         self.area = area         self.continent = continent     def getName(self):         return self.name     def getPopulation(self):         return self.pop     def getArea(self):         return self.area     def getContinent(self):         return self.continent     def setPopulation(self, pop):         self.pop = pop     def setArea(self, area):         self.area = area     def setContinent(self, continent):         self.continent = continent     def __repr__(self):         return (f'{self.name} (pop:{self.pop}, size: {self.area}) in {self.continent} ') TASK 2 Python Program: File: catalogue.py from Country...
c++ Program Description You are going to write a computer program/prototype to process mail packages that...
c++ Program Description You are going to write a computer program/prototype to process mail packages that are sent to different cities. For each destination city, a destination object is set up with the name of the city, the count of packages to the city and the total weight of all the packages. The destination object is updated periodically when new packages are collected. You will maintain a list of destination objects and use commands to process data in the list....
0. Introduction. In this laboratory assignment, you will write a Python class called Zillion. The class...
0. Introduction. In this laboratory assignment, you will write a Python class called Zillion. The class Zillion implements a decimal counter that allows numbers with an effectively infinite number of digits. Of course the number of digits isn’t really infinite, since it is bounded by the amount of memory in your computer, but it can be very large. 1. Examples. Here are some examples of how your class Zillion must work. I’ll first create an instance of Zillion. The string...
You are working for a company that is responsible for determining the winner of a prestigious...
You are working for a company that is responsible for determining the winner of a prestigious international event, Men’s Synchronized Swimming. Scoring is done by eleven (11) international judges. They each submit a score in the range from 0 to 100. The highest and lowest scores are not counted. The remaining nine (9) scores are averaged and the median value is also determined. The participating team with the highest average score wins. In case of a tie, the highest median...
The Business Case for Agility “The battle is not always to the strongest, nor the race...
The Business Case for Agility “The battle is not always to the strongest, nor the race to the swiftest, but that’s the way to bet ’em!”  —C. Morgan Cofer In This Chapter This chapter discusses the business case for Agility, presenting six benefits for teams and the enterprise. It also describes a financial model that shows why incremental development works. Takeaways Agility is not just about the team. There are product-management, project-management, and technical issues beyond the team’s control. Lean-Agile provides...
Please read the article and answear about questions. Determining the Value of the Business After you...
Please read the article and answear about questions. Determining the Value of the Business After you have completed a thorough and exacting investigation, you need to analyze all the infor- mation you have gathered. This is the time to consult with your business, financial, and legal advis- ers to arrive at an estimate of the value of the business. Outside advisers are impartial and are more likely to see the bad things about the business than are you. You should...
Point/Counterpoint from chapter 16. Take a stand. Do you agree or disagree? Write a minimum of...
Point/Counterpoint from chapter 16. Take a stand. Do you agree or disagree? Write a minimum of one paragraph. Chapter 16 The Hierarchical Structure: The Superior Format Point Yes The hierarchy is the enduring foundation for how MNEs optimally arrange the roles, responsibilities, and relationships of its structure for a simple reason—it is the superior format for doing so. It sets a clear chain of command, functional span of control, effective allocation of authority, and precise assignment of tasks. It specifies...
CSC 322 Systems Programming Fall 2019 Lab Assignment L1: Cipher-machine Due: Monday, September 23 1 Goal...
CSC 322 Systems Programming Fall 2019 Lab Assignment L1: Cipher-machine Due: Monday, September 23 1 Goal In the first lab, we will develop a system program (called cipher-machine) which can encrypt or decrypt a code using C language. It must be an errorless program, in which user repeatedly executes pre-defined commands and quits when he or she wants to exit. For C beginners, this project will be a good initiator to learn a new programming language. Students who already know...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT