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'
	os.system(command)
	print("[_] Reversed and copied!")

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

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.

First published Oct 20, 2009.

We totally get why you have an ad blocker. If you enjoy reading Geekswipe, turning it off for us helps keep the site alive and the science coming.

276 articles

Aeronautical engineer, product builder, developer, science fiction author, and an explorer. I'm the creator and editor of Geekswipe. I love writing about physics, aerospace, astronomy, and technology.

More by Karthikeyan KC

Leave a comment

Only used to notify you of replies. Never published.

Related