-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,259
  • 21
  • 28
  • 43
user2262031
  • 35
  • 2
  • 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
  • 171
  • 6
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