python - 从python中指定的字符串中获取n个字符
问题描述
让 s = "%2ABCDE" - 从字符串中获取前 2 个字符,然后输出应该是 "AB"。需要从字符串中由数字指定的字符串中获取字符。例如 s1="%12ABCDERTYUIOPLKHGF" - 从字符串中获取前 12 个字符。
我尝试使用 re.findall('\d+', string ) 获取数字,但如果我的字符串为“%2ABCD1”,则会出现问题。请建议
解决方案
s = "%2ABCDE"
number = ""
offset = 0
for i in range(len(s)):
if s[i] == "%":
continue
elif s[i].isdigit():
number += s[i]
else:
offset = i
break
print(s[offset:int(number) + offset])
输出:AB
推荐阅读
- swift - 更改每个用户在 firebase 中拥有的分支中的特定节点
- splunk - 如何制作包含按服务组织的两个事件的堆栈柱形图?
- javascript - 图片幻灯片随机化
- oracle - Oracle SQLLDR - 使用无效日期加载记录,用 Null 替换无效日期
- php - 尝试使用 jquery 删除表中不是第一行的行
- c# - UWP c# TreeView 带图标示例
- javascript - 将服务器端渲染添加到使用 create-react-app 创建的现有 React 站点
- niftynet - 广义骰子损失只学习一类
- html - 在引导按钮内放置自定义图标
- javascript - 如何测量 Node.js 中 JS 代码执行的 CPU 时间以进行基准测试?