Python Forum
Using a function: splitting, joining, and slicing a string
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Using a function: splitting, joining, and slicing a string
#1
Here is the homework task:

Quote:MASTER YODA: Given a sentence, return a sentence with the words reversed¶
master_yoda('I am home') --> 'home am I'
master_yoda('We are ready') --> 'ready are We'
Note: The .join() method may be useful here. The .join() method allows you to join together strings in a list with some connector string. For example, some uses of the .join() method:
>>> "--".join(['a','b','c'])
>>> 'a--b--c'
This means if you had a list of words you wanted to turn back into a sentence, you could just join them with a single space string:
>>> " ".join(['Hello','world'])
>>> "Hello world"

Here is the solution that I came up with:

def master_yoda(text):
    string = text.split()
    string.reverse()
    newstring = ' '.join(string)
    return newstring
This produces the expected output.;

Quote:master_yoda('I am home') --> 'home am I'
master_yoda('We are ready') --> 'ready are We'

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 newstring
But 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_string
With 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?
Reply


Messages In This Thread
Using a function: splitting, joining, and slicing a string - by Drone4four - Dec-26-2018, 02:14 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  List joining deatz 14 3,247 Dec-21-2022, 03:34 AM
Last Post: deatz
  slicing and counting words in a string Rudy 11 6,429 Mar-17-2019, 10:46 PM
Last Post: micseydel
  [split] Splitting a string into six pieces susmis666 1 2,218 Dec-25-2018, 06:16 PM
Last Post: ichabod801
  Quickest way of splitting a string yanhto 1 2,463 May-01-2018, 12:24 PM
Last Post: snippsat
  String Slicing fivestar 1 2,598 Nov-17-2017, 06:38 PM
Last Post: heiner55
  Splitting A String In Python Saif133 6 6,406 Jan-14-2017, 04:09 AM
Last Post: Saif133

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020