simple string revesal

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

simple string revesal

Postby venator87 » Sat Aug 03, 2013 12:53 am

I'm just doing a simple program where you enter a message and it prints it in reverse. When the program executes there are no errors and goes all the way through, but the message does not show. When I put the order as start:end, then the message will show (of course not reversed).


message = input("\nEnter a message to reverse: ")
start = 0
end = len(message)+ 1

print(message[end:start])

input("\nPlease press the enter key to exit.")
venator87
 
Posts: 5
Joined: Sat Mar 09, 2013 9:43 pm

Re: simple string revesal

Postby micseydel » Sat Aug 03, 2013 1:59 am

Think about it like a loop. If you are going to start at a value, and count up to it, then if you start above it then you're going to halt right away. Luckily there is a third part to slicing that is a step. Normally, it's 1, but you can set it to any integer you want, including negative ones. Also, they're all optional so you can just use the third and let the first and second default to start and end.
Join the #python-forum IRC channel on irc.freenode.net!
User avatar
micseydel
 
Posts: 1115
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA

Re: simple string revesal

Postby ochichinyezaboombwa » Sat Aug 03, 2013 4:30 am

makes me think of 4-5 ways of reversing a string in py... what about you guys?
ochichinyezaboombwa
 
Posts: 200
Joined: Tue Jun 04, 2013 7:53 pm

Re: simple string revesal

Postby micseydel » Sat Aug 03, 2013 6:11 am

There is a very standard one, which is what I attempted to describe.
Join the #python-forum IRC channel on irc.freenode.net!
User avatar
micseydel
 
Posts: 1115
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA

Re: simple string revesal

Postby metulburr » Sat Aug 03, 2013 12:23 pm

There is a very standard one, which is what I attempted to describe.

i guess ill just give the code
Code: Select all
s = 'some string'
print(s[::-1])
New Users, Read This
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
steam
User avatar
metulburr
 
Posts: 1305
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

Re: simple string revesal

Postby venator87 » Mon Aug 19, 2013 8:42 pm

Sorry for the late reply. Thank you for the help though! I was able to understand it after you explained it.
venator87
 
Posts: 5
Joined: Sat Mar 09, 2013 9:43 pm


Return to General Coding Help

Who is online

Users browsing this forum: Google [Bot], Mekire and 2 guests