Python will check the first or second line for an emacs/vim-like encoding specification.
More precisely, the first or second line must match the regular expression "coding[:=]\s*([-\w.]+)". The first group of this expression is then interpreted as encoding name. If the encoding is unknown to Python, an error is raised during compilation.
Source: PEP 263
(A BOM would also make Python interpret the source as UTF-8.
I would recommend, you use this over .decode('utf8')
# -*- encoding: utf-8 -*-
special_char_string = u"äöüáèô"
In any case, special_char_string will then contain a unicode object, no longer a str.
As you can see, they're both semantically equivalent:
>>> u"äöüáèô" == "äöüáèô".decode('utf8')
True
And the reverse:
>>> u"äöüáèô".encode('utf8')
'\xc3\xa4\xc3\xb6\xc3\xbc\xc3\xa1\xc3\xa8\xc3\xb4'
>>> "äöüáèô"
'\xc3\xa4\xc3\xb6\xc3\xbc\xc3\xa1\xc3\xa8\xc3\xb4'
There is a technical difference, however: if you use u"something", it will instruct the parser that there is a unicode literal, it should be a bit faster.
Answer from phant0m on Stack OverflowPython will check the first or second line for an emacs/vim-like encoding specification.
More precisely, the first or second line must match the regular expression "coding[:=]\s*([-\w.]+)". The first group of this expression is then interpreted as encoding name. If the encoding is unknown to Python, an error is raised during compilation.
Source: PEP 263
(A BOM would also make Python interpret the source as UTF-8.
I would recommend, you use this over .decode('utf8')
# -*- encoding: utf-8 -*-
special_char_string = u"äöüáèô"
In any case, special_char_string will then contain a unicode object, no longer a str.
As you can see, they're both semantically equivalent:
>>> u"äöüáèô" == "äöüáèô".decode('utf8')
True
And the reverse:
>>> u"äöüáèô".encode('utf8')
'\xc3\xa4\xc3\xb6\xc3\xbc\xc3\xa1\xc3\xa8\xc3\xb4'
>>> "äöüáèô"
'\xc3\xa4\xc3\xb6\xc3\xbc\xc3\xa1\xc3\xa8\xc3\xb4'
There is a technical difference, however: if you use u"something", it will instruct the parser that there is a unicode literal, it should be a bit faster.
Yes, this is the recommended way for Python 2.x, see PEP 0263. In Python 3.x and above, the default encoding is UTF-8 and not ASCII, so you don't need this there. See PEP 3120.
Please advise to complete the code if special characters in username print username not allowed, i am using replit for school if this helps.
special=('!','@','#','$','%','^','&','*','(',')','-','_','+','=','')
if special in Username:
print('username is incorrect')
You can fix the backslash by escaping it and ' can be fixed by putting it in double quotes:
symbols = {..., '\\', ... "'", ...}
But typing all this out is pretty tedious. Why not just use string.punctuation instead:
>>> from string import punctuation
>>> set(punctuation)
{'~', ':', "'", '+', '[', '\\', '@', '^', '{', '%', '(', '-', '"', '*', '|', ',', '&', '<', '`', '}', '.', '_', '=', ']', '!', '>', ';', '?', '#', '$', ')', '/'}
>>>
user2555451's answer is spot on, but just as an observation on how to get these into Python efficiently - the first part of your question - try this:
symbols = set(r"""`~!@#$%^&*()_-+={[}}|\:;"'<,>.?/""")
That:
- Wraps the string in three quotes to simplify handling the single or double quote issue
- Puts an
rin front of the string to handle the escape character setconverts the string to a set
The initial string was missing the close square bracket, just as an aside. Using a simple set operator:
>>> symbols - set(string.punctuation)
{']'}