473,396 Members | 1,792 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

String handling

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
5 1993
dshimer
136 Expert 100+
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
136 Expert 100+
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
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
136 Expert 100+
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
ghostdog74
511 Expert 256MB
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

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

Similar topics

6
by: lkrubner | last post by:
Last year I asked a bunch of questions about character encoding on this newsgroup. All the answers came down to using ord() in creative ways to try to make guesses about multi-byte characters. I...
4
by: Ken Fine | last post by:
No joy on Macromedia's boards after a few days; maybe someone can help me here. I got an excellent string handling function off of planet-source-code.com that converts text strings to proper...
4
by: Rajeev | last post by:
Hi I am a VB programmer. I need to do a simple thing in VC++. char strNWUserName; DWORD dBufferLength = 256; HRESULT hr; hr = WNetGetUserA("DOMAIN", strNWUserName, &dBufferLength); The...
1
by: Derek | last post by:
A common technique for trimming leading and trailing spaces from std::string is the following: string s(" blah blah blah "); const char* ws= " \t\r"; string::size_type not_white; // trim...
6
by: rakesh | last post by:
Hi, I have a requirement in which I need to search a string in a file stored on a harddisk. String which needs to be searched would be stored in a file with delimiter as new line character. some...
17
by: Chad Myers | last post by:
I've been perf testing an application of mine and I've noticed that there are a lot (and I mean A LOT -- megabytes and megabytes of 'em) System.String instances being created. I've done some...
32
by: tshad | last post by:
Can you do a search for more that one string in another string? Something like: someString.IndexOf("something1","something2","something3",0) or would you have to do something like: if...
4
by: WaterWalk | last post by:
Hello, I'm currently learning string manipulation. I'm curious about what is the favored way for string manipulation in C, expecially when strings contain non-ASCII characters. For example, if...
11
by: Peter Kirk | last post by:
Hi i have a string variable which can take one of a set of many values. Depending on the value I need to take different (but related) actions. So I have a big if/else-if construct - but what is...
12
by: kevineller794 | last post by:
I want to make a split string function, but it's getting complicated. What I want to do is make a function with a String, BeginStr and an EndStr variable, and I want it to return it in a char...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.