Need help with a string plz! (newbie)

I

israphelr

Hi all

So I created a program, that gets a string from a user and then prints
the string in reverse order.

Now, here's the code;

#################################
print "\nWelcome !"
print "\nEnter a word, and the world will be reversed!"

word = raw_input("\nPlease Enter a word: ")

end = len(word)
end -= 1

for position in range(end, -1, -1):
print word[position],

raw_input("\nPress Enter to Exit")

################################

What actually happens is, the next character is printed on the same
line but, there is a space inbetween each character.

I thought maybe I could create another variable and then assign each
character into the new string by concatenating, makign a new string
each time, but I find this a bit muddling at the moment. Any help'd be
great. Thanks.
 
M

Marc 'BlackJack' Rintsch

israphelr said:
So I created a program, that gets a string from a user and then prints
the string in reverse order.

In [57]: 'reversed string'[::-1]
Out[57]: 'gnirts desrever'

Ciao,
Marc 'BlackJack' Rintsch
 
G

Grant Edwards

#################################
print "\nWelcome !"
print "\nEnter a word, and the world will be reversed!"

word = raw_input("\nPlease Enter a word: ")

end = len(word)
end -= 1

for position in range(end, -1, -1):
print word[position],

raw_input("\nPress Enter to Exit")

################################

What actually happens is, the next character is printed on the same
line but, there is a space inbetween each character.

Yup, that's what print does. Try sys.stdout.write(word[position]).
You'll have to import sys, of course.

or try this:

print ''.join(reversed(list(word)))

or this:

print word[-1::-1]

I would have sworn there was a string method that returned the
reversed string, but there doesn't seem to be in 2.4.3...
I thought maybe I could create another variable and then assign each
character into the new string by concatenating, makign a new string
each time, but I find this a bit muddling at the moment. Any help'd be
great. Thanks.

s = ''
for c in word:
s = c + s
print s
 
I

israphelr

s = ''
for c in word:
s = c + s
print s


Okay thanks very much, I used this method, since it's cloest one to I
am already faimilar with.

Here's the final code..(if anyones interested)

######################################################

print "\nWelcome !"
print "\nEnter a word, and the world will be reversed!"

word = raw_input("\nPlease Enter a word: ")

end = len(word)
end -= 1
new_string = ""

for position in range(end, -1, -1):
new_string += word[position]

print new_string

raw_input("\nPress Enter to Exit")

#######################################
 
L

Larry Bates

s = ''
for c in word:
s = c + s
print s


Okay thanks very much, I used this method, since it's cloest one to I
am already faimilar with.

Here's the final code..(if anyones interested)

######################################################

print "\nWelcome !"
print "\nEnter a word, and the world will be reversed!"

word = raw_input("\nPlease Enter a word: ")

end = len(word)
end -= 1
new_string = ""

for position in range(end, -1, -1):
new_string += word[position]

print new_string

raw_input("\nPress Enter to Exit")

#######################################

If you are really trying to learn Python, you really owe it to
yourself to study Grant's replies. You are using constructs
from some previous language that you learned and trying to
apply them to Python. IMHO Python programmers would never use
s=c+s on strings as it is quite inefficient. The methods
and functions he uses reversed(), .join(), list() are very
powerful Python constructs that you will need to learn how
to use. The slicing word[-1::-1] is also something that
will come in handy later if you learn how to use it. This
is a simple example that you can learn a lot from if you
will study Grant's responses.

Just my two cents.

-Larry
 
I

israphelr

Okay thanks very much, I used this method, since it's cloest one to I
am already faimilar with.
Here's the final code..(if anyones interested)

print "\nWelcome !"
print "\nEnter a word, and the world will be reversed!"
word = raw_input("\nPlease Enter a word: ")
end = len(word)
end -= 1
new_string = ""
for position in range(end, -1, -1):
new_string += word[position]
print new_string
raw_input("\nPress Enter to Exit")
#######################################

If you are really trying to learn Python, you really owe it to
yourself to study Grant's replies. You are using constructs
from some previous language that you learned and trying to
apply them to Python. IMHO Python programmers would never use
s=c+s on strings as it is quite inefficient. The methods
and functions he uses reversed(), .join(), list() are very
powerful Python constructs that you will need to learn how
to use. The slicing word[-1::-1] is also something that
will come in handy later if you learn how to use it. This
is a simple example that you can learn a lot from if you
will study Grant's responses.

Just my two cents.

-Larry

Oh, thanks for the advice then. And as for Grant..look forward to
seeing more of your posts.
 
G

Grant Edwards

Thanks for that - I was wondering where you got hold of the skewed
thinking which I enjoy reading so much.

I grabbed the file full of quotes from the emacs distribution
back around 1990, so that's why the pop-culture references are
a bit dated. I don't know if emacs still includes Zippy quotes
(of if they've been updated), but you used to be able to do
"esc-X yow" and emacs would show you a random Zippy quote.
 
B

Bruno Desthuilliers

Grant Edwards a écrit :
(snip)
I don't know if emacs still includes Zippy quotes
(of if they've been updated), but you used to be able to do
"esc-X yow" and emacs would show you a random Zippy quote.
It's still there.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top