-2

enter image description hereenter image description hereenter image description hereI am trying to assign user = 'corp\adam'

Using python I am unable to create a user variable as desire. Desired Output:

user

'corp\adam'

I don't want to print the variable. I need to store it.

Zebra
  • 139
  • 2
  • 10

4 Answers4

2

In Python (and commonly in other programming languages too) the backslash character is used to denote special characters that could not be typed directly into a string. This is known as an escape sequence. To specify a literal backslash, use it twice:

user = 'corp\\adam'

0

Try a double backslash, i.e. corp\\adam. The first backslash denotes that the second one has to be evaluated like a normal character, not as another escape character.

A. Galloway
  • 135
  • 1
  • 10
  • This may also be helpful: https://stackoverflow.com/questions/17327202/python-replace-single-backslash-with-double-backslash – A. Galloway Jun 17 '17 at 00:09
0

You can use r'corp\adam' that way you tell Python it's a "raw" string and the backslash will not be used to escape other characters.

From the docs:

Both string and bytes literals may optionally be prefixed with a letter 'r' or 'R'; such strings are called raw strings and treat backslashes as literal characters.

Mike Scotty
  • 10,530
  • 5
  • 38
  • 50
  • r'corp\adam' results in output = 'corp\\adam' . I tried just know – Zebra Jun 17 '17 at 00:08
  • @GauravP: That's because you output the `repr` of the string (you probably just type the variable's name in the interactive interpreter). If you did `print(output)` the extra backslash wouldn't be there. – ShadowRanger Jun 17 '17 at 00:09
  • @GauravP That is the `repr` of the string, not the string itself. Using either a raw string or the double-backslash is the correct answer – juanpa.arrivillaga Jun 17 '17 at 00:09
  • @juanpa.arrivillage - i need to use the variable later on as a password field. I don't want to print it. – Zebra Jun 17 '17 at 00:15
  • @GauravP yes, so? What is it that you are not understanding? If you want, inspect the string character by character and you'll see it is *exactly what you want*. You are confused about the `repr` – juanpa.arrivillaga Jun 17 '17 at 00:15
  • config = dict(server = 'servername' , username = 'crop\adam') . Now i need to use this config variable in conn = pyodbc.connect( r'DRIVER={ODBC Driver 13 for SQL Server};' + conn_str.format(**config) ) – Zebra Jun 17 '17 at 00:21
  • however when you try to see what config is username = 'corp\\adam' and not 'corp\adam' – Zebra Jun 17 '17 at 00:21
  • @GauravP You need `config = dict(server = 'servername' , username = r'crop\adam')`. Or `config = dict(server = 'servername' , username = 'crop\\adam')`. Either one of those gives you what you need – juanpa.arrivillaga Jun 17 '17 at 00:23
  • Can you paste the screenshot by trying it ? I have tried what you mentioned but it doesn't work. – Zebra Jun 17 '17 at 00:24
  • I uploaded the screenshot . Please check – Zebra Jun 17 '17 at 00:26
  • This is getting boring. Please write the string to a file or to a DB or something, but stop saying that it does not work by looking at the ``repr`` of the string instead of the actual character values. – Mike Scotty Jun 17 '17 at 00:30
  • @GauravP Yes I see it. And it is *correct*. I and others have already explained to you why you are seeing the extra backslash in the terminal output. – juanpa.arrivillaga Jun 17 '17 at 00:31
  • I am sorry for this and i agree with you . Now i uploaded a screenshot. How do i ensure that my pyodbc read the username as corp\adam and not as corp\\adam – Zebra Jun 17 '17 at 00:39
  • @GauravP are you trolling? It is doing that already in your screenshot. – juanpa.arrivillaga Jun 17 '17 at 00:50
  • dude - it says Login failed for 'corp\\adam' . It is sending a wrong string to the odbc connection. It should be sending 'corp\adam' . Are you not getting me ? Database is receiving 'corp\\adam' as a username string while it should be receiving 'corp\adam' – Zebra Jun 17 '17 at 01:00
  • @GauravP dude, I'm almost *certain* that that's because the error message prints the *representation* of the string. The quotes are a dead give-away. Try giving it `username="test\bname"`. What does the error message print now? – juanpa.arrivillaga Jun 17 '17 at 03:21
  • @GauravP here are the three possibilities, I think. 1) your working environment has been cluttered by all the debugging you are doing. Somewhere, somehow, `config` is being shadowed with the wrong parameters. 2) whatever sql server library you are using is post-processing the string somehow, messing it up 3) the login is failing for some other reason. However, regardless, **the string you have assigned to that `config` dictionary is the string you are asking for**. – juanpa.arrivillaga Jun 17 '17 at 03:25
  • @GauravP If you do narrow the issue down, ask another question or edit this one but you have give *way more* detail about the issue. As it stands, the question you are asking here is *answered*. – juanpa.arrivillaga Jun 17 '17 at 03:26
0

The backslash character acts as an escape when the next character is an ASCII or Python special escape character. Either escape the backslash with another backslash:

 'corp\\adam'

or use a raw string (where backslash only escapes the quote character it doesn't even escape itself):

 r'corp\adam'
ShadowRanger
  • 143,180
  • 12
  • 188
  • 271