combining strings with slash seperater

Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Jun 2006
Posts: 187
Reputation: Matt Tacular is an unknown quantity at this point 
Solved Threads: 7
Matt Tacular's Avatar
Matt Tacular Matt Tacular is offline Offline
Unverified User

combining strings with slash seperater

 
0
  #1
May 29th, 2007
  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
Reply With Quote Quick reply to this message  
Join Date: Sep 2005
Posts: 133
Reputation: mawe is an unknown quantity at this point 
Solved Threads: 58
mawe mawe is offline Offline
Junior Poster

Re: combining strings with slash seperater

 
0
  #2
May 29th, 2007
Hi!

  1. print "\\".join((str1, str2))
or
  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
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 311
Reputation: BearofNH is on a distinguished road 
Solved Threads: 40
BearofNH's Avatar
BearofNH BearofNH is offline Offline
Posting Whiz

Re: combining strings with slash seperater

 
1
  #3
May 29th, 2007
  1. >>> str3 = str1 + '\\' + str2
  2. >>> print str3
  3. c:\documents and settings\user\desktop\starcraft.exe
  4. >>>
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 151
Reputation: ghostdog74 is on a distinguished road 
Solved Threads: 40
ghostdog74 ghostdog74 is offline Offline
Junior Poster

Re: combining strings with slash seperater

 
1
  #4
May 31st, 2007
Originally Posted by Matt Tacular View Post
  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.
  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.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 2380 | Replies: 3
Thread Tools Search this Thread



Tag cloud for Python
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC