Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
Edit= Some moderators recommended me to make my self more clear, so here we go.
As a personal project in python, I'm making a very simple software that asks the user for an email address and then checks if the syntaxis of the email is correct.
I made a tuple of special characters that are not allowed in an email address, one of those characters is "\". I was looking online like crazy for how to make \ into a str with no result. I try looking online for the use of the function \ with no result either.
V = "\" doesn't work, it gives me a syntax error. I know it is possible to make it into a string because I've done it with an Input() command.
Please help.
It's not clear to me what language you're using - but in most cases you need to escape the backslash, as it is an escape character itself.
V="\\"
This functionality exists that you can include special characters (in this case, a double quote) in the string:
V="The following will be in quotes: \"Hello, World\""
In this case, the escaped double quotes will be treated as literal characters in the string, and will not signal the end of the string as they would without the escape character.
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed last year.
Improve this question
my_variable_name = str("John")
print(my_variable_name)
This is the code for example, now if I happen to add the double quotes around "my_variable_name" in this statement:
print(my_variable_name)
It just simply display whatever is written inside the "" but if I don't add it'll print "john". Now what I think the reason is because when compiler find something inside "" that tells it to display whatever datatype it is while without "" it just displays the stored or u can say assigned value . I know its easy (basis) but I do this like all the time and my knowledge about this problem never satisfies me
The problem has nothing to do with print function.
"my_variable_name" is a string literal. Single or triple quotes could also be used.
my_variable_name is a reference to a previously defined variable of that name. The type of the variable's value could be anything.
You can print any object, and it'll return the str() representation of it.
Unrelated, you don't need str() function to define a string literal.
when compiler find something inside "" that tells it to display whatever datatype it is
Python is an interpreted language, it's not the compiler doing this. The datatype of anything enclosed in quotes is always a string
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
I want to replace every caret character with a unicode superscript, for nicer printing of equations in python. My problem is, every caret may be followed by a different exponent value, so in the unicode string u'\u00b*', the * wildcard needs to be the exponent I want to print in the string. I figured some regex would work for this, but my experience with that is very little.
For example, supposed I have a string
"x^3-x^2"
, I would then want this to be converted to the unicode string
u"x\u00b3-x\u00b2"
You can use re.sub and str.translate to catch exponents and change them to unicode superscripts.
import re
def to_superscript(num):
transl = str.maketrans(dict(zip('1234567890', '¹²³⁴⁵⁶⁷⁸⁹⁰')))
return num.translate(transl)
s = 'x^3-x^2'
out = re.sub('\^\s*(\d+)', lambda m: to_superscript(m[1]), s)
print(out)
Output
x³-x²
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
I'm using pymysql to query a database that has an entry like 'name':'Te\xtCorp', it's a name that I need to preserve. I'm sending it somewhere else with json.dumps() and when it hits this it fails to escape the \x.
What's the proper way to escape the \x without double escaping everything else?
Two options here:
You escape the backslash, like:
'Te\\xtCorp'
You can use a raw string:
r'Te\xtCorp'
Both generate:
>>> 'Te\\xtCorp'
'Te\\xtCorp'
>>> r'Te\xtCorp'
'Te\\xtCorp'
Or printed:
>>> print(r'Te\xtCorp')
Te\xtCorp
Note that in order to inspect the content of the string, you should use a print(..) statement, otherwise you get the repr(..)esentation of that string. For example:
>>> print(json.dumps(r'te\xt'))
"te\\xt"
>>> print(json.loads(json.dumps(r'te\xt')))
te\xt
As one can read in the documentation on String literals:
\xhh...: ASCII character with hex value hh...
So it is used to encode any ASCII character, by specifying the code as a hexadecimal value.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I can use other escape characters without any problem but my atom text editor and python itself doesn't see it as an escape character but as a normal character.
print '\s', test_line
just writes
\stesting_bot1
How can I make it so that the editor and python will see this as an escape character and as space ?
\s isn't an escape sequence in Python. \t, \n, \r etc are (see the Python lexical analysis docs) but non-special characters will not be interpreted as anything special, hence your \s appearing literally.
However, \s does means space in regular expression syntax of course...
I think you may be confusing a regex with a string. For a normal string, you just need to use the space character to print it:
print(' testing_bot1')
\s is not an escape sequence, so it will be interpreted as just backslash + "s".
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I currently work on a project in python and I would have liked to make a SQL query with an apostrophe in my character string. Ex: "Jimmy's home."
And I have this error:
1064 You have an error in your SQL syntax
So, I've tried to put a \ before the apostrophe, but when I look at my database, just the strings before the \' are in my field.
String to in query: "Jimmy\'s home"
String in Database: "Jimmy"
I don't understand why?
As has been posted here and elsewhere many many times before, you must not use string interpolation to build up SQL strings. Use parameters intead:
REQ = u"INSERT INTO organismes (NAME_organisme`,ID_organisme,adresse,cp,town,lat,lon,tel,fax,email,website) VALUES (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s);"
cursor.execute(REQ, (NAME,ID,LIGNE,CP,TOWN,LAT,LON,TEL,FAX,MAIL,URL))
Note, no quotes around the placeholders in the query string: the db adapter puts them in for you as necessary.
Also, please don't use capital letters for things that are not constants.
You need use "Jimmy''s" to get your result"Jimmy's" instead of "Jimmy\'s" in SQL.
because ' or " is a special key, when you put it in your query the sql will automatically find where is the other ' or " that's why there's what we called a escape keycode which is \ the use of this is to display the next character in original value