Python根据长度和字符查找子字符串

问题描述

我正在尝试根据长度和/或字符关联获取子字符串。

字符串看起来像这样。 字符串 = " 1.11

我想以某种方式反转字符串,以便字母数字子字符串在右侧,整数在右侧。

我试过了 string1 = (string[6:16]) 等但不起作用,因为整数可以超过 3 个字符。

解决方法

您可以拆分字符串,然后按所需顺序再次组合。

string = " 1.11 << Z99E004Z "
[string1,string2] = string.split("<<")
result_string = string2 + "<<" + string1
,
istr = " 1.11 << Z99E004Z "
pos = istr.find(" ",2)
str1 = istr[pos:] + istr[0:pos]
print("Input: ",istr)
print("Output: ",str1)istr = " 1.11 << Z99E004Z "

输出:

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