943,910 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Unsolved
  • Views: 4469
  • Python RSS
May 29th, 2007
0

combining strings with slash seperater

Expand Post »
Python Syntax (Toggle Plain Text)
  1. str1 = 'c:\documents and settings\user\desktop'
  2. str2 = 'starcraft.exe'
  3. 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
Similar Threads
Reputation Points: 10
Solved Threads: 7
Unverified User
Matt Tacular is offline Offline
187 posts
since Jun 2006
May 29th, 2007
0

Re: combining strings with slash seperater

Hi!

Python Syntax (Toggle Plain Text)
  1. print "\\".join((str1, str2))
or
Python Syntax (Toggle Plain Text)
  1. 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
Reputation Points: 19
Solved Threads: 58
Junior Poster
mawe is offline Offline
133 posts
since Sep 2005
May 29th, 2007
1

Re: combining strings with slash seperater

Python Syntax (Toggle Plain Text)
  1. >>> str3 = str1 + '\\' + str2
  2. >>> print str3
  3. c:\documents and settings\user\desktop\starcraft.exe
  4. >>>
Reputation Points: 94
Solved Threads: 48
Posting Whiz
BearofNH is offline Offline
321 posts
since May 2007
May 31st, 2007
1

Re: combining strings with slash seperater

Python Syntax (Toggle Plain Text)
  1. str1 = 'c:\documents and settings\user\desktop'
  2. str2 = 'starcraft.exe'
  3. 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.
Python Syntax (Toggle Plain Text)
  1. dir = os.path.join("C:\\","documents and settings","user","desktop")
  2. starcraftpath = os.path.join(dir,"starcraft.exe")
takes care of the slashes for you.
Reputation Points: 75
Solved Threads: 44
Junior Poster
ghostdog74 is offline Offline
156 posts
since Apr 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: can't figure out loop
Next Thread in Python Forum Timeline: Too many questions.





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC