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

String handling

P: 18
Hello, I need some specific string related functionality and I find string functions in Python to be bit lacking. I am not sure if their are other functions that I can use. I did manage to write some code that seems to work but I am afraid it might not work as expected in all cases.

Lets say, I have following variables,

myString="RLFEGDNALIR" #some string
myFrag="NALIR" #known substring of above string

From these information, I would like to:

1) Get the
otherFrag= "RLFEGD" #complimentary fragment/substring of myFrag

2) Check if their is a specific letter within two spaces of the "fragmentation site" in otherFrag

I could not find any string functions that returned substrings except for split which I was able to make it work for me but it is not satisfactory at all and I am afraid their are bugs from exceptional cases that I might be overlooking. This i what I do right now:

1)
otherFrag=st.rsplit(myString,myFrag,1)[0]

2)
proxCheck=(len(otherFrag)-1)-st.rfind(otherFrag,'specific_letter') #proximity check
if (proxCheck==1) or (proxCheck==2):
multipleFlag=0

I guess I am just wondering if their are more powerful substring methods in python that I can use besides "split" functionality in string. For instance, something that returns substring of a string if I supply the starting and ending position. I suppose I can write a module myself.. hmm
Jan 11 '07 #1
Share this Question
Share on Google+
5 Replies


dshimer
Expert 100+
P: 136
I haven't thought about part 2 yet, but the slice method seems to work for part 1.

Expand|Select|Wrap|Line Numbers
  1. myString[0:string.find(myString,myFrag)]
  2. returns 'RLFEGD'
myString="RLFEGDNALIR" #some string
myFrag="NALIR" #known substring of above string

From these information, I would like to:

1) Get the
otherFrag= "RLFEGD" #complimentary fragment/substring of myFrag
Jan 11 '07 #2

dshimer
Expert 100+
P: 136
Then for part 2, though it is basically just another way of doing what you already have other than getting it in one line.

Expand|Select|Wrap|Line Numbers
  1. if specific_letter == otherFrag[len(otherFrag)-1] or specific_letter == otherFrag[len(otherFrag)-2]:
  2.   print 'true'

2) Check if their is a specific letter within two spaces of the "fragmentation site" in otherFrag

2)
proxCheck=(len(otherFrag)-1)-st.rfind(otherFrag,'specific_letter') #proximity check
if (proxCheck==1) or (proxCheck==2):
multipleFlag=0
Jan 11 '07 #3

P: 18
Then for part 2, though it is basically just another way of doing what you already have other than getting it in one line.

Expand|Select|Wrap|Line Numbers
  1. if specific_letter == otherFrag[len(otherFrag)-1] or specific_letter == otherFrag[len(otherFrag)-2]:
  2.   print 'true'

Ok, I think your solution for line 2 is better than mine even though its same concept. I think, unlike my solution, yours would work even if other fragment is only one or two letters long.

Added later: Although, a more robust solution would be better. I might want to expand the tolerance on proximity and might want to look six spaces - which would mean more if conditions. But for now it works
Jan 11 '07 #4

dshimer
Expert 100+
P: 136
In which case, if it were me, I would write an eval function that use something like the following pseudocode.

Expand|Select|Wrap|Line Numbers
  1.  
  2. for letter in range(HoweverManyYouWant):
  3.     if specific letter==letter at that position:
  4.         return true
  5.  
Added later: Although, a more robust solution would be better. I might want to expand the tolerance on proximity and might want to look six spaces - which would mean more if conditions. But for now it works
Jan 11 '07 #5

Expert 100+
P: 511
1) Get the
otherFrag= "RLFEGD" #complimentary fragment/substring of myFrag
How about just replacing with "" judging from your required output.
Expand|Select|Wrap|Line Numbers
  1. otherFrag=myString.replace(myFrag,'')
  2.  
2) Check if their is a specific letter within two spaces of the "fragmentation site" in otherFrag

I could not find any string functions that returned substrings except for split which I was able to make it work for me but it is not satisfactory at all and I am afraid their are bugs from exceptional cases that I might be overlooking. This i what I do right now:
substrings are implemented using index slicing in Python. eg astring[0:3] get first 3 characters. judging from your requirements, i think you could just use index slicing.
Jan 12 '07 #6

Post your reply

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