Reverse a String and Copy to Clipboard with Python


Sometimes over IM chats, I love to annoy my friends. Either by messing up the text message or replacing them entirely with emojis. Here is one such fun script that’ll reverse the given string and copy it to the Windows clipboard.

import os

def clip_it(msg):
	command = 'echo ' + msg.strip() + '| clip'
	print("[_] Reversed and copied!")

message = input("[_] Message: ")
reversed = message[::-1]

This is a trivial script. But for beginners, here is a brief overview of what’s happening. We’re using the extended slice syntax [start:stop:step] of Python. If you leave the start and stop empty and assign the step to be -1, the string will be reversed and assigned to reversed.

Then we pass reversed to a method that echoes the reversed message and pipes it to the clip command of Windows, which will copy the string into the Windows clipboard.

This post was first published on October 20, 2009.


Karthikeyan KC

Aeronautical engineer, dev, science fiction author, gamer, and an explorer. I am the creator of Geekswipe. I love writing about physics, aerospace, astronomy, and python. I created Swyde. Currently working on Arclind Mindspace.

Leave a Reply

Your email address will not be published. Required fields are marked *