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
#2
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
Reply
#3
(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()
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  List joining deatz 14 3,114 Dec-21-2022, 03:34 AM
Last Post: deatz
  slicing and counting words in a string Rudy 11 6,300 Mar-17-2019, 10:46 PM
Last Post: micseydel
  [split] Splitting a string into six pieces susmis666 1 2,190 Dec-25-2018, 06:16 PM
Last Post: ichabod801
  Quickest way of splitting a string yanhto 1 2,420 May-01-2018, 12:24 PM
Last Post: snippsat
  String Slicing fivestar 1 2,554 Nov-17-2017, 06:38 PM
Last Post: heiner55
  Splitting A String In Python Saif133 6 6,283 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