-1

I'm trying to get a substring based on length and or character association.

The string looks like this. string = " 1.11 << Z99E004Z "

I want to somehow invert the string so the alphanumeric substring is on the right and integer is on the right.

I've tried string1 = (string[6:16]) etc but doesn't work as the integer can be more than 3 characters.

General Grievance
  • 4,555
  • 31
  • 31
  • 45
user2262031
  • 45
  • 1
  • 3
  • 7
  • If I am not wrong, you only want to [invert the string right](https://stackoverflow.com/questions/931092/reverse-a-string-in-python)? – Jdeep Apr 21 '21 at 13:09
  • I assume you mean "the alphanumeric substring is on the left and the integer is on the right." Although `1.11` is actually not an integer. If the form and length of the substrings won't always be the same, check out [regular expressions](https://docs.python.org/3/howto/regex.html) and use them to match the alphanumeric substring and the number. I think you'll be able to see what you need to do from there. – TheSprinter Apr 21 '21 at 13:10
  • 1
    This is really unclear. Show the expected result. – Manuel Apr 21 '21 at 13:12

2 Answers2

0

You could split the string and then combine it again in the desired order.

string = " 1.11 << Z99E004Z "
[string1, string2] = string.split("<<")
result_string = string2 + "<<" + string1
chillking
  • 311
  • 1
  • 9
  • Thanks, that should work , one thing I failed to mention was the "<<" could also be a number of characters > < >= =< == – user2262031 Apr 21 '21 at 13:17
  • Can actually be split on whitespace, seems to work thanks – user2262031 Apr 21 '21 at 13:22
  • Exactly, you could use space as separator, then you would need to adjust to string combining at the end because the resulting list would have five entries. – chillking Apr 21 '21 at 14:06
0
istr = " 1.11 << Z99E004Z "
pos = istr.find(" ",2)
str1 = istr[pos:] + istr[0:pos]
print("Input: ", istr)
print("Output: ", str1)istr = " 1.11 << Z99E004Z "

Output:

Input:   1.11 << Z99E004Z 
Output:   << Z99E004Z  1.11