Python Forum
printing a string in reverse
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
printing a string in reverse
#1
i have a string in a variable x. i want to print that string in reverse order. like:
Output:
>>> x = 'foobar' >>> print(rev(x)) raboof >>>
... assuming rev() does the job for me. but if there is no rev()], what to code? i am not interested in answers without working code. i have tried reversed() and sorted(...,reverse=True) and have not yet found a nice compact solution.

if someone wants to implement rev(), try to make sure it will reverse the order of any sequence and return the very same type it was given..
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#2
If it's a string, then you can create a reversed string by slicing with a -1 step..

>>> x = "foobar"
>>> print(x[::-1])
raboof
ghoul and Skaperen like this post
Reply
#3
(Nov-20-2021, 12:54 AM)bowlofred Wrote: If it's a string, then you can create a reversed string by slicing with a -1 step..

>>> x = "foobar"
>>> print(x[::-1])
raboof

That looks super handy. I would have taken the route to manually reverse the string myself
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Printing a raw string with a folder separator at the end, duplicates the separator krulah 5 1,164 Nov-28-2022, 12:41 PM
Last Post: snippsat
  printing an string instead of a expression Underscore 2 1,781 Oct-11-2021, 03:10 PM
Last Post: deanhystad
  I am trying to reverse a string using loop codinglearner 4 2,128 Sep-28-2021, 10:46 PM
Last Post: Pedroski55
  Printing string at specific position on terminal - not showing __Mathieu__ 1 2,331 Sep-07-2020, 10:32 AM
Last Post: Larz60+
  Reverse a String ragav_in 3 2,137 Jul-24-2020, 02:24 AM
Last Post: ragav_in
  Reverse the string word sneha 2 2,568 Dec-12-2019, 03:37 AM
Last Post: sneha
  printing a bytes string Skaperen 2 2,308 Jul-21-2019, 03:42 AM
Last Post: Skaperen
  String slicing in python from reverse ift38375 1 2,358 Apr-29-2019, 06:58 AM
Last Post: perfringo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020