1

As the title says, I am confused on how to put back the string into a line after iterating through it. Right now I have this:

for element in want_cyphered:
    element = ord(element)
    element = element + 13
    element = chr(element)
    print(element)

This iterates through the want_cyphered string and prints it. This is a function I'm using for a cipher and decipherer. What I was wondering is how to compile the iteration into an actual line again? Right now, my print is one character per line, but I want it to be all on one line. Any help would be appreciated! Using python.

John Mee
  • 50,179
  • 34
  • 152
  • 186
ReptiDelts
  • 19
  • 1
  • 3
    You could also iterate in a comprehension: `''.join(chr(ord(element) + 13) for element in want_cyphered)`. You might also be interested in the [rot13 codec in the standard library](https://github.com/python/cpython/blob/3.8/Lib/encodings/rot_13.py). – kojiro Aug 04 '20 at 23:55
  • Append/concatenate the strings? – AMC Aug 05 '20 at 00:04
  • Does this answer your question? [Concatenate item in list to strings](https://stackoverflow.com/questions/12453580/concatenate-item-in-list-to-strings) – AMC Aug 05 '20 at 00:05

5 Answers5

3

The other answers aren't wrong, but since you're using Python, you may as well use what it's good at (and your code is likely to be faster as well):

want_cyphered = 'Some text'
cyphered = ''.join(chr(ord(ch) + 13) for ch in want_cyphered)
print(cyphered)

decyphered = ''.join(chr(ord(ch) - 13) for ch in cyphered)
print(decyphered )

To break that down (assuming you're new at Python): ''.join(list_of_parts) takes a list of string parts (characters or strings) and joins them together into a single string, using whatever the string at the start is - an empty string in this case.

You can generate that list of parts using a generator expression (a very well performing way of iterating over something iterable) like [ch for ch in some_str], which would get you a list of characters in a string.

I've put the generator in square brackets just now so that it would become an actual list, but when you only write a generator to use as the input to some function, you can just pass the generator itself, without the brackets like ''.join(ch for ch in some_str) - which basically does nothing. It takes the string apart and puts it back together again.

But you can apply operations to the elements of that generator as well, so instead of just ch, you could fill the list with chr(ord(ch) + 13) which is the cypher you were looking to apply.

Put all that together and you get:

cyphered = ''.join(chr(ord(ch) + 13) for ch in want_cyphered)
Grismar
  • 27,561
  • 4
  • 31
  • 54
  • Note that you'd probably want to add the cypher modulo some value to get the cypher text - it's fine for just text, but if you were to use a really high number instead of 13, or you'd cypher a character near the end of the character set, it would break. That's not the question though, so I left it out of the solution. – Grismar Aug 05 '20 at 00:05
2

Create a variable and append your element to the variable inside the loop. When the loop ends print the result.

result = ""
for element in want_cyphered:
    element = ord(element)
    element = element + 13
    element = chr(element)
    result += element
print(result)

Or if you want to get really fancy...

print("".join([chr(ord(e) + 13) for e in want_cyphered]))
John Mee
  • 50,179
  • 34
  • 152
  • 186
1

just do

for element in want_cyphered:
    element = ord(element)
    element = element + 13
    element = chr(element)
    print(element,end="")

the end="" part tells Python to add a empty string at the end. The end paramter defaults to a new line so it prints each character on one line. Setting end="" makes it all print on one line.

aidan0626
  • 307
  • 3
  • 8
1

Just make an empty string and on each iteration, add the element to it!

want_cyphered = "Hello, World!"
cyphered = ""

for element in want_cyphered:
    element = ord(element)
    element = element + 13
    element = chr(element)
    # Add the element to string
    cyphered += element
M-Chen-3
  • 2,036
  • 5
  • 13
  • 34
0

Here is an approach that uses the translate, make_trans functions.

from string import ascii_lowercase as lc, ascii_uppercase as uc

def rot_13(s):
    return s.translate(str.maketrans(lc+uc, lc[13:]+lc[:13]+uc[13:]+uc[:13]))

s = 'Hello World!'

print(rot_13(s))

Prints

Uryyb Jbeyq!
Chris Charley
  • 6,403
  • 2
  • 24
  • 26