Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

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

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading