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 ?

6
  • 1
    "I have tried this a number of ways" - the ways you tested that seems to be secret. So the answer is secret as well... Commented May 22, 2013 at 10:32
  • When you say "end up getting", do you mean when you print it? and chuckle @glglgl Commented May 22, 2013 at 10:33
  • You might want to try print c and print len(c). c only has one backslash. Commented May 22, 2013 at 10:43
  • You've made no attempt to remove duplicate `` characters Commented May 22, 2013 at 10:44
  • Working for me using python 2.6.6 Commented May 22, 2013 at 13:31

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank Yossi..but when I print the concatenated string to a file, I get the output 'INTRANET\\jDoe'..maybe I am missing something here.
Yossi, my problem was still not resolved..I updated the question.
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'

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.