str1 = 'c:\documents and settings\user\desktop'
str2 = 'starcraft.exe'
print str1

I would like to know how it would be possible to add str2 to the end of str1 with a "\" inbetween the two strings. so that I end up with "c:\documents and settings\user\desktop\starcraft.exe".

Thanks

Hi!

print "\\".join((str1, str2))

or

print "%s\\%s" % (str1, str2)

If you want to make this os-independent, have a look at the os module (os.path.join might be your friend :))

Regards, mawe

>>> str3 = str1 + '\\' + str2
>>> print str3
c:\documents and settings\user\desktop\starcraft.exe
>>>
commented: good ideas +5
str1 = 'c:\documents and settings\user\desktop'
str2 = 'starcraft.exe'
print str1

I would like to know how it would be possible to add str2 to the end of str1 with a "\" inbetween the two strings. so that I end up with "c:\documents and settings\user\desktop\starcraft.exe".

Thanks

one method i always use is the os.path.join() method.

dir = os.path.join("C:\\","documents and settings","user","desktop")
starcraftpath = os.path.join(dir,"starcraft.exe")

takes care of the slashes for you.

commented: very insightful +5
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.