Search code examples
pythonarrayslistencodingrot13

How to store a list of letters in an variable as a string


So my homework assignment is to write a program which performs a ROTn encodement. Meaning shifting a string of letters by the value n. For example: if n = 2, then "a" would be "c" when encoded. We should store the shifted string in a variable.

So I just started this lecture, so we didn't learn a lot in python yet. The problem should be solvable without the need to import stuff.

So my idea was to shift every letter on its own and then store it an array. Then I could output it as a string with print(''.join(my_array). But for the automatic correction system, the shifted string should be stored in a variable, which causes a problem for me. I don't know how.

if __name__ == "__main__":
    plain_text = "abc"
    shift_by = 1

# perform a ROTn encoding

plain_text = input("Please enter a password: ")
shift_by = int(input("Enter a value to shift the password: "))

store_shift = []
x = 0

for n in plain_text:
    if n.isalpha():
        n = chr(ord(n) + shift_by)
        store_shift.append(n)
        x += 1

encoded = ... #here should be the shifted string
print(''.join(store_shift)

Please ignore my bad variable names. Feel free to also correct stylistic mistakes etc.

To sum up; I'm unable to store an array of letters in an variable as a string. For example; array["a", "b", "c"] should be stored in variable = abc (as a string)


Solution

  • convert the array to a list then use:

    ''.join(some_list)

    for example:

    some_list = ['a','b','c']
    some_string = ''.join(some_list)
    print(some_string)
    #'abc'