Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
ascii question
#1
In the homework section, a Cesar cypher is presented.
A string of symbols is provided:
SYMBOLS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890 !?.'
But as you can see, this is far from complete on the punctuation side, thus forcing us to write code,
in case a symbol from the cypher is not found. Fine, you can do that. (e.g. comma, hyphen...)
I thought of doing this, to make list more complete and generic:
import string
symbols = '  ' + string.ascii_letters + string.digits + string.punctuation
Output:
abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~
Only, the string .punctuation contains both ' and ", making the program wonder where the symbols string starts and ends.
Of course it is possible to choose and "pop" one before adding string.punctuation to symbols, but is there a better way?
(Make a list instead?)
Paul
It is more important to do the right thing, than to do the thing right.(P.Drucker)
Better is the enemy of good. (Montesquieu) = French version for 'kiss'.
Reply
#2
You can use a triple quoted string
>>> SYMBOLS = """abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~"""
>>> 
>>> SYMBOLS
'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~'
Also for a better looking code, use lexical string concatenation
>>> SYMBOLS = (
...     """abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"""
...     """!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~""")
Reply
#3
>>> import string
>>> 
>>> symbols = f"""{string.ascii_letters}{string.digits}{string.punctuation}"""
>>> symbols
'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~'
>>> print(symbols)
abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~
Reply
#4
you and also use string
import string


SYMBOLS = f"{string.ascii_lowercase}{string.ascii_uppercase}{string.punctuation}"
print(SYMBOLS)
Output:
'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~'
see: https://docs.python.org/3/library/string...-constants
Reply
#5
Plenty of choice.
Thanks,
Paul
It is more important to do the right thing, than to do the thing right.(P.Drucker)
Better is the enemy of good. (Montesquieu) = French version for 'kiss'.
Reply
#6
And another choice.
import string

print("".join((string.ascii_letters, string.digits, string.punctuation)))
Output:
abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~
Reply
#7
One question, 4 viable answers.
I'm Blush
Paul
It is more important to do the right thing, than to do the thing right.(P.Drucker)
Better is the enemy of good. (Montesquieu) = French version for 'kiss'.
Reply
#8
using this \ before u want to print out strings symbol like ' or ".
symbols = 'banana \' apple'
print(symbols)
Output:
banana ' apple
Reply
#9
Thread name consists ascii. So lazy person like me would do:

>>> ''.join(chr(i) for i in range(32, 127))
' !"#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~'
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#10
If you are not worried about the numbers being first you could use, the slice is trimming of the whitespace characters tab, linefeed, return, formfeed, and vertical tab.
import string

print(string.printable[:-5])
Output:
0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~
Edit: changed the slice to 5 so it includes a space on the end
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020