Question

In: Computer Science

Write a function called format_name that accepts a string in the format of first name followed...

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:

  • A main method that uses user input to prompt and read a string that contains a person's first name and last name
  • A call to format_name and a print statement that prints the output returned by format_name
  • A call to the main function

PLEASE HELP PYTHON COMPUTER SCIENCE

Solutions

Expert Solution

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


Related Solutions

Complete the Python function called 'greetings(time)' that accepts a time in "HH:MM" format as a string....
Complete the Python function called 'greetings(time)' that accepts a time in "HH:MM" format as a string. The function should return a greeting message based on the hour given in the time object as follow: If the time is before noon: 'Good Morning.', if it is in the afternoon: 'Good Day.' Please use the following template for your python script to define the greetings() function and name it as mt_q3.py. Replace the place holder [seneca_id] with your Seneca email user name....
Write a function called remove_punct() that accepts a string as a parameter, removes the punctuation (',',...
Write a function called remove_punct() that accepts a string as a parameter, removes the punctuation (',', '!', '.') characters from the string, and returns the number of punctuation characters removed. For example, if the string contains ['C', 'p', 't', 'S', ',', '1', '2', '1', '.', 'i', 's', 'f', 'u', 'n', '!', '\0'], then the function should remove the punctuation characters. The function must remove the characters by shifting all characters to the right of each punctuation character, left by one...
Using Python #Write a function called after_second that accepts two #arguments: a target string to search,...
Using Python #Write a function called after_second that accepts two #arguments: a target string to search, and string to search #for. The function should return everything in the first #string *after* the *second* occurrence of the search term. #You can assume there will always be at least two #occurrences of the search term in the first string. # #For example: # after_second("11223344554321", "3") -> 44554321 # #The search term "3" appears at indices 4 and 5. So, this #returns everything...
Write a function called fnReadInParameters() that takes as parameter(s): • a string indicating the name of...
Write a function called fnReadInParameters() that takes as parameter(s): • a string indicating the name of the parameter le This function should return a dictionary that stores all of the parameter le data of the SIR model in a format that we will describe further below.
Write a python function to fulfill the requirements. The function accepts a string, a current state,...
Write a python function to fulfill the requirements. The function accepts a string, a current state, edges’ information, and an accepting state. The output of the function is a boolean value verifying if the string can pass the finite state machine or not.             ### Finite State Machine Simulator in Python ### Provide s1 and s2 that are both accepted, but s1 != s2. s1 = "bdf" s2 = "bdgbdf" edges = {(1,'a') : 2,                (1,'b') : 3,       ...
In Java, write a recursive function that accepts a string as its argument and prints the...
In Java, write a recursive function that accepts a string as its argument and prints the string in reverse order. Demonstrate the function in a driver program.
3. Write a function named "countNonAlpha" that accepts a string. It will return the number of...
3. Write a function named "countNonAlpha" that accepts a string. It will return the number of non-alphabet characters (excluding blanks) in the string. For example, if the string is "Hello, World!", it will return 2 for ',' and '!" in the string. 4. Write a function named "deleteZeros" that takes two arguments: a. an array of integer values; b. an integer for the number of elements in the array; The function will return the number of zeros that it has...
Your code needs to do the following: Create a function called pigLatin that accepts a string...
Your code needs to do the following: Create a function called pigLatin that accepts a string of English words in the parameter sentence and returns a string of those words translated into Pig Latin. English is translated to Pig Latin by taking the first letter of every word, moving it to the end of the word and adding ‘ay’. For example the sentence “The quick brown fox” becomes “hetay uickqay rownbay oxfay”. You may assume the words in the parameter...
Code in c# Write a recursive method called isReverse(String s1, String s2) that accepts two strings...
Code in c# Write a recursive method called isReverse(String s1, String s2) that accepts two strings as parameters and returns true if the two strings contain the same sequence of characters as each other but in the opposite order and false otherwise. • The recursive function should ignore capitalization. (For example, the call of isReverse("hello", "eLLoH") would return true.) • The empty string, as well as any one letter string, should be its own reverse. Write a driver program that...
-Write in C++ -Use Char library functions Write a function that accepts a string representing password...
-Write in C++ -Use Char library functions Write a function that accepts a string representing password and determines whether the string is a valid password. A valid password as the following properties: 1. At least 8 characters long 2. Has at least one upper case letter 3. Has at least one lower case letter 4. Has at least one digit 5. Has at least on special character
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT