In: Computer Science
Write a function called format_name that accepts a string in the format of first name followed by last name as a parameter, and then returns a string in reverse order (i.e., last name, first name). You may assume that only a first and last name will be given.
For example, format_name("Jared Smith") should return "Smith, Jared"
Hint: The following String Methods will be useful:
# Returns the lowest index in the string where substring is # found. Returns -1 if substring is not found my_string = “eggplant” index = my_string.find(“plant”) # returns 3 # Returns all the characters after the specific index my_string = "hello world!" print my_string[1:] # returns "ello world!" print my_string[6:] # returns "world!" # Returns all the characters before the specific index my_string = "hello world!" print my_string[:6] # returns "hello" print my_string[:1] # returns "h"
Your program should include:
PLEASE HELP PYTHON COMPUTER SCIENCE
The following code has been written in Python 3. For testing purpose, we have considered the same example name and shown the output after executing the code
Source code:
# Following function returns the formatted name
def format_name(name):
first_name, last_name = name.split()
formatted_name = last_name + ", " + first_name
return formatted_name
# Main function
def main():
name = input("Enter the person's name: ")
print("The formatted name is: %s" % format_name(name))
if __name__ == "__main__":
main()
Picture of source code along with sample input and sample output:
As you can see output in the above terminal
1 2 3 4 5 6 7 8 9 10 . Following function returns the formatted name def format_name(name): first name, last name = name.split() formatted_name = last_name + " + first_name return formatted_name # Main function def main(): name = raw_input("Enter the person's name: ") print "The formatted name is: ** $ format_name(name) if 12 13 name main() 15 PROBLEMS OUTPUT DEBUG CONSOLE TERMINAL Enter the person's name: Jared Smith The formatted name is: Smith, Jared