Using a function: splitting, joining, and slicing a string - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Homework (https://python-forum.io/forum-9.html) +--- Thread: Using a function: splitting, joining, and slicing a string (/thread-14950.html) |
Using a function: splitting, joining, and slicing a string - Drone4four - Dec-26-2018 Here is the homework task: Quote:MASTER YODA: Given a sentence, return a sentence with the words reversed¶ Here is the solution that I came up with: def master_yoda(text): string = text.split() string.reverse() newstring = ' '.join(string) return newstringThis produces the expected output.; Quote:master_yoda('I am home') --> 'home am I' Hooray! However I wanted to take this script one step further by adding proper punctuation so that “ready” is changed to “Ready”. And “home” is adjusted so that it reads “Ready”. I have set out to achieve this by invoking both the lower and capitalize casting methods. My new script looks like this: def master_yoda(text): string = text.split() string.reverse() newstring = ' '.join(string) newstring[0:-1].lower() # Casting the entire string to be lowercase newstring[0].capitalize() # Casting the first letter to be uppercase return newstringBut the output is the same as before. “ready” and “home” still remain uncapitalized. What is going on at lines 5 and 6? What would you people recommend I use instead? At this point the course-task is complete so I am ready for the full answer from someone here. For my future reference, the course material can be found here: Pierian-Data/Complete-Python-3-Bootcamp. The specific sub- module I am working on is: 03-Methods and Functions/03-Function Practice Exercises.ipynb EDIT: I made some further progress: Rather than letting the casting methods sit there, I just assigned them to a new variable. Here is what my script looks like now: def master_yoda(text): string = text.split() string.reverse() newstring = ' '.join(string) lowered_string = newstring[0:-1].lower() # Casting the entire line to be lowercase final_string = lowered_string[0].capitalize() # Casting the first letter to be uppercase return final_stringWith master_yoda('I am home') I am still expecting “Home am i” as the output but now all I am getting is ‘H’. Likewise, with master_yoda('We are ready') I am also expecting “Ready are we” yet I am getting just: “R”. Why? RE: Using a function: splitting, joining, and slicing a string - stullis - Dec-26-2018 On line 6, you're setting final_string to a capitalized first letter of the string. The remainder of the string is not included. You could correct it by concatenating the rest of the string to the first letter. def master_yoda(text): string = text.split() string.reverse() newstring = ' '.join(string) lowered_string = newstring[0:-1].lower() # Casting the entire line to be lowercase final_string = lowered_string[0].capitalize() + lowered_string[1:] return final_string RE: Using a function: splitting, joining, and slicing a string - perfringo - Dec-27-2018 (Dec-26-2018, 04:33 PM)stullis Wrote:def master_yoda(text): string = text.split() string.reverse() newstring = ' '.join(string) lowered_string = newstring[0:-1].lower() # Casting the entire line to be lowercase final_string = lowered_string[0].capitalize() + lowered_string[1:] return final_string Some observations. Naming must not be misleading. It could be confusing to have list with name string. No need to slice string or lowercase it, capitalize will deliver desired results: >>> help(str.capitalize) Help on method_descriptor: capitalize(self, /) Return a capitalized version of the string. More specifically, make the first character have upper case and the rest lower case. (END) >>> 'we ARE Ready'.capitalize() 'We are ready'So this function can be expressed as: def master_yoda(text): string = text.split() string.reverse() return ' '.join(string).capitalize()If one wishes, this function can be shortened even more and expressed as one-liner: def master_yoda(text): return ' '.join(reversed(text.split())).capitalize() |