Mohammad Hassani
Mohammad Hassani

Reputation: 55

Python printing format in a for loop

*if menuInput == '1':
     print("")
     encryptString = input("Please enter string to encrypt:")
     print("")
     offSetValue = int(input("Please enter offset value (1-94):"))
     print("")
 
     for character in encryptString:
         x = ord(character)
         y = x + offSetValue
         z = chr(y)      
         print("Encrypted string:") 
         print(z)*

I want the string that has been encrypted to be outputted in this format:

Encrypted string:

abcde 

but it comes out in this format:

Encrypted string: 
a
Encrypted string: 
b
Encrypted string: 
c
Encrypted string: 
d

I've tried using end and sep but they don't help.

Upvotes: 1

Views: 61

Answers (3)

NullUsxr
NullUsxr

Reputation: 43

By using fernet, it allows you to encrypt messages more securely than shifting the position of characters in the string.

from cryptography.fernet import Fernet
if menuInput == '1':
    print()
    encryptString = input("Please enter string to encrypt:")
    print()
    key = Fernet.generate_key()
    fernet = Fernet(key)
    encryptedString = fernet.encrypt(encryptString.encode())
    print("Encrypted string:", encryptedString)

Upvotes: 1

user3366507
user3366507

Reputation:

I think this is what you want.

print("")
encryptString = input("Please enter string to encrypt:")
print("")
offSetValue = int(input("Please enter offset value (1-94):"))
print("")

print("Encrypted string:") 
for character in encryptString:
    x = ord(character)
    y = x + offSetValue
    z = chr(y)      
    print(z, end="")

print()

You want print("Encrypted string:") before the loop and use end instead of sep. sep is used to separate multiple arguments in the print. end is the terminating character(s) added to the end of what is printed. Here we terminate with the empty string. The default for end is the newline character, \n. That is why you saw everything printed on a new line.

Upvotes: 1

Janaka Chathuranga
Janaka Chathuranga

Reputation: 1840

Here we go.

if menuInput == '1':
 print("")
 encryptString = input("Please enter string to encrypt:")
 print("")
 offSetValue = int(input("Please enter offset value (1-94):"))
 print("")

 print("Encrypted string:") 
 for character in encryptString:
     x = ord(character)
     y = x + offSetValue
     z = chr(y)      
     print(z, end='')

Upvotes: 0

Related Questions