Jump to content

Simple String manipulation!

- - - - -

  • Please log in to reply
5 replies to this topic

#1
madskillsmonk

madskillsmonk

    Newbie

  • Members
  • PipPip
  • 11 posts
Hi, im working on a program where the user enters a sentance. I then, have to display the sentance 1 word at a time, beginning with the last word and so on... Example:
sentance = "Hello i am a python programmer!"
output:
programmer
python
a
am
i
hello


MY CODE SO FAR:

sentance = raw_input("Enter a sentance\n")

spaces = []

for letter in sentance:

    if letter == ' ':

        spaces.append(letter)


for i in range(len(spaces)):

    print sentance[spaces[i:i+1]]

can anyone help me?
thanks!

Edited by ZekeDragon, 08 March 2011 - 06:01 PM.
Please use [CODE] tags (the # button) when posting code.


#2
madskillsmonk

madskillsmonk

    Newbie

  • Members
  • PipPip
  • 11 posts
anyone?

sentance = raw_input("Enter a sentance\n")

spaces = []

for letter in sentance:

    if letter == ' ':

        spaces.append(letter)


for i in range(len(spaces)):

    print sentance[spaces[i:i+1]]

this is what i have so far... and i dont know what it isnt working... if someone could help me i would be veryyy thankful!
thanks in advance

Edited by ZekeDragon, 08 March 2011 - 06:01 PM.


#3
ZekeDragon

ZekeDragon

    Writes binary right handed and hex left handed

  • Moderators
  • 2,103 posts
Hey, hold your horses yo, I hadn't even had a chance to read it yet. :P

All right, the way you'd want to do this is by splitting the input string by spaces, then clean up each string of special characters, capital letters, etc. Then just reverse the list and print it out!

user_input = input("Enter a sentence.\n")

words = user_input.split(' ')

clean_words = []


for word in words:

    for n in range(len(word)):

        if not word[n:n+1].isalpha():

            word = word[:n] + word[n+1:]

    if word == "": continue # Just filter out empty strings.

    clean_words.append(word.lowercase())
Then print it reversed.
# This is why one tests one's code after not using a language for 6+ months:

#

# for word in clean_words.reversed():

#

# Preserved for posterity.


for word in reversed(clean_words):

    print word
Warning: I program to Python 3, it appears you're programming to Python 2 as you're using raw_input. Even though I used input() in my implementation, Python 3's input is the same as Python 2's raw_input.

I very intentionally left this with as little to go on as possible, since what I want you to do is write your own implementation using what you should eventually understand as what's going on in the code. I looked at your code and it appears that you may not even understand how to use for loops to loop over the elements of a sequence. There's a lot of built-in methods in Python that simplify the code you write in it, as well as some nifty shortcuts (though I didn't find one for one of my purposes on this loop). Could you tell me how long you have been programming in Python?

Edited by ZekeDragon, 09 March 2011 - 06:40 PM.

Wow I changed my sig!

#4
madskillsmonk

madskillsmonk

    Newbie

  • Members
  • PipPip
  • 11 posts
the problem with what you gave me, is that im getting an error, saying list has no attribues "reversed"?
not sure why..
but, i could do the same program using built in methods myself, im sorry i forgot to specify, i want to avoid using methods. my initial aproach was to find the spaces in the sentance, and then put the number of the string when they appear into the list, and then loop through the list, begining at the end, and print out each different spot.
if that makes sense to you lol.
ive been programmig python for just a couple weeks at school now

#5
Flying Dutchman

Flying Dutchman

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 890 posts
  • Location:::1
Python has different, more fun approach to programming. Afterwards I can show you the the one liner, I did it two lines. Like ZekeDragon said, you can split the sentence by space, and then print them in reverse order in lower case.

Hint: you can reverse a list like you can reverse a string!
A conclusion is where you got tired of thinking.
#define class struct    // All is public.

#6
restored

restored

    Newbie

  • Members
  • PipPip
  • 14 posts
I'll append a small example

>>> print(list(reversed("aa bb cc dd".split())))

['dd', 'cc', 'bb', 'aa']

>>> print(*list(reversed("aa bb cc dd".split())))

dd cc bb aa

>>> print(*list(reversed("aa bb cc dd".split())), sep='\n')

dd

cc

bb

aa

>>>






1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users