Python - concatenate a string to include one backslash

In Python 2.6, I need to create a string by combining INTRANET\ and userid, for example jDoe , to get the string INTRANET\jDoe . This line will be part of the SQL query. I tried this in several ways, but in the end I get INTRANET\\jDoe , and so my query returns no results.

I want to do this:

 a = 'INTRANET\\' b = 'jDoe' c = a+b ### want to get c as 'INTRANET\jDoe', not 'INTRANET\\jDoe' 

thanks


The problem seems a little different:

When I type c, I get "INTRANET \ jDoe". But when I add c to the list (which will be used in the sql query), as shown below:

 list1 = [] list1.append(c) print list1 >>>['INTRANET\\jDoe'] 

Why is this?

+4
source share
2 answers

Additional \ exists due to python escaping.

 >>> print 'INTERNET\\jDoe' INTERNET\jDoe 

This does not affect the SQL that you are using. You should look at a different direction.

+2
source

Try using the following code,

 s1 = "INTRANET" s1 = s1 + "\\" s1 = s1 + "jDoe" print s1 

This will give the correct output INTERNET\jDoe

If you just try to view the contents of a variable, you will see an extra \, which is an escape sequence in python. In this case, he will show

 'INTERNET\\jDoe' 
+1
source

Source: https://habr.com/ru/post/1482132/


All Articles