Exercise: Name Reverser

Life is 10% what happens to us and 90% how we react to it. If you don't build your dream, someone else will hire you to help them build theirs.

Exercise: Name Reverser

write a Python program to get a user to input a first and last name, print the names reverses, but in the same order. For example, if the user input Graham Smith, the output would be maharG htimS.

In my solution, there are two methods that Both of them start off with a name variable being assigned by the input function with a prompt to the user to put their first and the last name to reverse. So when the user types in their text, such as Graham Smith, the name variable will hold that data. So we see that in both the first solution and in the second solution. Both also take advantage of then splitting that name on a space. In the first example, this is done explicitly, so that words variable will hold a list with the first name and the last name in it. In the second example, the first variable and the last variable will hold the first name and the last name. In the second example, the split method of that string object is just using the default split parameter, which is to split on whitespace.

 #First Method
 name = input ('First and last name to reverse -> ! ') 
 words =name.split("") 
 for word in words:        
	lastindex = len(word) -1
	for index in range(lastindex, -1, -1):
               print(word[index], end="")        
			   print(end='')
	print(end='\n') 
 #Second Method
 name = input ('First and last name to reverse -> ! ')
 first, last  = name.split() 
 print(first[::-1], last[::-1])