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.
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)
Enter a string to encrypt: Hello World
Encrypted string: Khoor Zruog
- 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