By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,272 Members | 1,733 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,272 IT Pros & Developers. It's quick & easy.

How to change part of a variable (string)?

P: n/a
I have a variable "fileName" = ****.pdb, where the *s are capitalised letters. After a method uses this variable as an argument, I need to change the extension part to .ent without changing the first part. For some reason, none of the str.replace methods seem to work.
Oct 27 '10 #1
Share this Question
Share on Google+
3 Replies


Expert 100+
P: 622
none of the str.replace methods seem to work.
Yes they do. Post what you have tried and someone will tell you where you went wrong.
Oct 27 '10 #2

Expert 100+
P: 391
If you know it's always that format you can just use:
Expand|Select|Wrap|Line Numbers
  1. newstring=fileName[:-3]+"ent"
Oct 28 '10 #3

bvdet
Expert Mod 2.5K+
P: 2,851
Keep in mind strings are immutable. You must reassign the string returned by the replace method to an identifier.
Expand|Select|Wrap|Line Numbers
  1. >>> s = "somestring.txt"
  2. >>> s.replace("txt", "xyz")
  3. 'somestring.xyz'
  4. >>> s
  5. 'somestring.txt'
  6. >>> news = s.replace("txt", "xyz")
  7. >>> news
  8. 'somestring.xyz'
Oct 28 '10 #4

Post your reply

Sign in to post your reply or Sign up for a free account.