Middle of word problem

This is the place for queries that don't fit in any of the other categories.

Middle of word problem

Postby carlh » Fri Oct 04, 2013 7:51 pm

Define a function 'middle' that returns the 'middle" of each word in a string, where the middle is obtained by removing the 1st and last
character from the word. For instance, middle("stop") returns ’to’. Note that for corner cases of words with two or one characters,
the middle is an empty string. Here is an example of middle with a string containing several words.

Code: Select all
>>> T = "an internet server company"
>>> middle(T)
'nterne erve ompan'

Please help for my homework due at midnight...I am so lost :(
Posts: 2
Joined: Fri Oct 04, 2013 7:41 pm

Re: Middle of word problem

Postby Yoriz » Sat Oct 05, 2013 9:41 am

Use an if statement to return a blank string if there is zero or 1 characters in the string.
Use string slicing to return the string with the first and last character removed.
New Users, Read This
Join the #python-forum IRC channel on irc.freenode.net!
User avatar
Posts: 1634
Joined: Fri Feb 08, 2013 1:35 am
Location: UK

Re: Middle of word problem

Postby stranac » Sat Oct 05, 2013 1:53 pm

You would have to use string slicing(as suggested by Yoriz) on every word of the input.

You will have to split the string into a list of words, apply slicing to every item, and then join them back into a single string.
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
Posts: 1790
Joined: Thu Feb 07, 2013 3:42 pm

Re: Middle of word problem

Postby micseydel » Sat Oct 05, 2013 7:31 pm

In the future, when you have something you need help with (especially immediate help) don't just copy-paste your assignment and say you're lost. Show us your best attempt, what it does accomplish (since we know from the assignment what it should do) and why you think it should work. That way, we can help to correct any misunderstandings you have so that for future assignments you'll be more prepared instead of having to come back again. (And of course if you do need to come back that's alright, but the idea is to get you better at programming.)
Join the #python-forum IRC channel on irc.freenode.net for off-topic chat!

Please prefer not to PM members. The point of the forum is so that anyone can benefit. We don't want to help you over PMs/emails/Skype chats that others can't benefit from :)
User avatar
Posts: 2919
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA

Return to General Coding Help

Who is online

Users browsing this forum: Google [Bot] and 4 guests