TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

<< Back to PYTHON

Python ROT13 Method

Implement the ROT13 cipher. Shift characters back and forward 13 places.
ROT13. The ROT13 algorithm obscures text. It does not encrypt it. The algorithm shifts each character back, or forward, 13 places. It is a cipher algorithm that can deter unwanted examination. We implement it with Python.Strings
Example. Many Python constructs are needed to implement ROT13. We define a method called rot13(). We use the for-loop to iterate over the string characters. And then we call the ord() built-in function.

So: We compare each integer representation against lowercase and uppercase letters. We subtract or add 13 to shift characters.

And: We append to the result string the character representation of the integer. We call chr() to do this.

Python program that does rot13 transformation def rot13(s): result = "" # Loop over characters. for v in s: # Convert to number with ord. c = ord(v) # Shift number back or forward. if c >= ord('a') and c <= ord('z'): if c > ord('m'): c -= 13 else: c += 13 elif c >= ord('A') and c <= ord('Z'): if c > ord('M'): c -= 13 else: c += 13 # Append to result. result += chr(c) # Return transformation. return result # Test method. print(rot13("gandalf")) print(rot13(rot13("gandalf"))) Output tnaqnys gandalf
Notes, tests. We test to make sure the rot13() method is correct. We find that it correctly translates the word "gandalf." When we call rot13() on the result of rot13(), we again have our original string value.

Thus: The rot13() algorithm round-trips its data correctly. The original data is not lost. This is correct.

Discussion. The ROT13 algorithm is an important one to implement. It offers no security. But it is a handy way to add obscurity—text that is not easily readable. This is useful in certain situations.

Also: The algorithm helps teach us how to treat characters as numbers. In Python we do this with ord() and chr(), two built-in functions.

Translate: A faster implementation is possible using the translate method. We benchmark and implement one.

Translate
Summary. The ROT13 algorithm deters unwanted snooping. It is not encryption. But in many cases, encryption is not worthwhile—it adds extra complexity and programming burden. Ciphers, such as ROT13, offer a simpler alternative.
© TheDeveloperBlog.com
The Dev Codes

Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf