0

I am trying to create a system that will automatically (and securely) create a grid with exactly 16 columns that represents every possible character a user could input into an input field; the issue is that whitespaces (like tabs, new lines, etc) are printing as a normal new line character escape sequence would. ex :

    ~(p{][Iu\k#hd-
    ebZ24>8<=)'zQAgM
    inr?s`/V0vJ;tc6L
    1oPyG}CK:&5x37Da
    RO!.wYqFSl
    9@fN_*  <---- not 16 characters
    $j,BT^|WH
    m+%E

The real issue isnt that it is on multiple lines, as I have up to somewhere around 25, but if it is treating the white spaces as escape codes and not characters then if the user inserts an escape code it wont be found in this grid which would end up causing issues. Is there a solution to this issue as nothing I can think of makes sense and any search terms I have tried have been dry. (it should be noted that part of the way I am printing them on multiple lines is by adding a newline character to the end of every sequence of 16 characters, this makes it even more difficult as my first idea was to just use raw string or something, but if I do that it wont print on new lines without several print functions and the end goal of this obviously isnt to just print the text onto a terminal)

import string
try:
  import secrets
except:
  !pip install secrets
  import secrets
#self.charlist = list(" {}{}{}".format(string.ascii_letters,string.digits,string.punctuation)) #my old method which didnt include whitespaces
self.charlist = list(string.printable)
print(self.charlist)
self.randomizedlist = []
while self.charlist:
  self.temp = secrets.randbelow(len(self.charlist))
  self.randomizedlist.append(self.charlist[self.temp]) 
  self.charlist.pop(self.temp)
print(self.randomizedlist)
self.n = 16
self.penfinal = [self.randomizedlist[self.i * self.n:(self.i + 1) * self.n] for self.i in range((len(self.randomizedlist) + self.n - 1) // self.n )] # a one liner I found online to split the list
print(self.penfinal)
self.finallist = []
for self.I in range(len(self.penfinal)):
  self.finallist.append("".join(self.penfinal[self.I]))
print(self.finallist)
self.finalstring = ""
for self.I in range(len(self.finallist)):
  self.finalstring += self.finallist[self.I] + "\n"
print(self.finalstring)
Paul na
  • 1
  • 1
  • Does this answer your question? [How to print a string literally in Python](https://stackoverflow.com/questions/6903551/how-to-print-a-string-literally-in-python) – b_c Nov 08 '19 at 16:24
  • It sounds like you want a (single) printable character to use as the display version of each character. You might try the Control Pictures block (U+2400 through U+2426); e.g., map the linefeed to U+240A, ␊. – chepner Nov 08 '19 at 18:57

0 Answers0