Part of the series

Several example codes

~2 min read • Updated Sep 22, 2025

Program Overview

This Python program takes a string input from the user, encrypts it using a simple algorithm, and displays the encrypted result.
It’s a great way to introduce basic encryption concepts and string manipulation in Python.


Python Code (Caesar Cipher Example):


def encrypt(text, shift=3):
    result = ""
    for char in text:
        if char.isalpha():
            base = ord('A') if char.isupper() else ord('a')
            result += chr((ord(char) - base + shift) % 26 + base)
        else:
            result += char
    return result

# Get input from user
input_text = input("Enter a string to encrypt: ")
encrypted = encrypt(input_text)
print("Encrypted string:", encrypted)

Sample Output:


Enter a string to encrypt: Hello World  
Encrypted string: Khoor Zruog

Explanation:

- The encrypt() function uses a Caesar cipher to shift each letter by a fixed number of positions
- Uppercase and lowercase letters are handled separately to preserve case
- Non-letter characters (spaces, punctuation) remain unchanged
- The final encrypted string is printed to the console


Written & researched by Dr. Shahin Siami