A: 

\\ is an escape sequence which prints \ as the output. If you want to print C:\\, you'll have to use C:\\\\ as the input string(or use raw strings ...). I can't see why you would want that. Although if you particularly want to, there are different options available.

batbrat
I want it... because this path is then sent to another application (in the for loop I call another app)
apt
Ok! Thanks for clarifying.
batbrat
A: 

you need \\? you can use print repr(pth)

RC
thanks RC.. I got it working with repr(pth)... but that piece of code looks ugly and need a better alternative: pth1 = repr(pth) ..pth1 looks like "'C:\\'" ... pth2 = pth[1:-1] .. to get rid of single quotes from pth1!
apt
+2  A: 

The double slash is simply string escaping - you need to escape slashes in string literals. Printing lst[0] before the loop will print it without the slash. If you want to really include a double slash in your literal, use the raw string syntax:

>>> lst = ['C:\\', 'C:\\Windows', 'C:\\Program Files']
>>> lst[0]
'C:\\'
>>> print lst[0]
C:\
>>> lst2 = [r'C:\\', r'C:\\Windows', r'C:\\Program Files']
>>> lst2[0]
'C:\\\\'
>>> print lst2[0]
C:\\

EDIT: If you want to double the slashes, you can do a simple string replace:

>>> x = 'C:\\Windows'
>>> print x
C:\Windows
>>> x = x.replace('\\', '\\\\')
>>> print x
C:\\Windows
Max Shawabkeh
how do I create a raw string within the list? e.g. for l in lst: r = raw_string from l?
apt
A raw string is simply a way to represent a string literal. There's no difference between it and a normal string after it is evaluated. If you explain what you are trying to achieve, we may be able to help you with that though.
Max Shawabkeh
hi Mark, I have updated my question to explain what I need thanks!
apt
A: 

If possible, try to use os.path.join() for creating your windows path. You don't have to meddle with slashes as much.

eg

from os.path import join
rootdir="C:\\"
path1 = join(rootdir,"windows")
path2 = join(rootdir,"Program Files")
lst = [ rootdir , path1, path2 ]
ghostdog74
A: 

Use / for sanity on windows (most programs work with both forms of slashes), but failing that, use r'' whenever you are dealing with backslashed path names.

r'C:\My\Windows\Path'

If you really want double backslashes, then that works too:

r'C:\\My\\Escaped\\Path'
Paul Hankin
yes.. raw string is the answer here.. but how do I create it within the loop? for l in lst: l1 = raw string using l ??
apt
+2  A: 

In Python, when you say

>>> s = 'C:\\'

s contains three characters: C, : and \. This can be easily seen by:

>>> len(s)
3

In Python, as in many other languages, a backslash is used to escape certain characters. For example, a newline is \n, a character with value 0 is \x00, etc. A "real" backslash is \\. So, to actually get two backslashes, you need to escape both, giving:

>>> s = 'C:\\\\'

But, Windows is perfectly happy with / as the separator, so you can do:

>>> s = 'C:/'
Alok