5

In Python 2.6, I need to create a string by concatenating INTRANET\ and a userid such as jDoe to obtain a string INTRANET\jDoe. This will string will be a part of a SQL query. I have tried this a number of ways but end up getting INTRANET\\jDoe and hence my query does not return any 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 print c, I get 'INTRANET\jDoe'. But when I append c to a list (to be used in a sql query) as below:

list1 = []
list1.append(c)
print list1

>>>['INTRANET\\jDoe']

Why is this ?

sunny
  • 141
  • 1
  • 2
  • 8

2 Answers2

2

The additional \ is there due to python escaping.

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

It doesn't affect the SQL you are using. You should look at another direction.

Yossi
  • 11,778
  • 2
  • 53
  • 66
  • Thank Yossi..but when I print the concatenated string to a file, I get the output 'INTRANET\\jDoe'..maybe I am missing something here. – sunny May 22 '13 at 10:56
  • Yossi, my problem was still not resolved..I updated the question. – sunny May 22 '13 at 14:24
1

Try the following code,

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

This will give the correct output INTERNET\jDoe

If you simply try to view the content of the variable you will see an extra \, which is an escape sequence in python. In that case it will show,

'INTERNET\\jDoe'
Deepu
  • 7,592
  • 4
  • 25
  • 47