ThinkChat🤖让你学习和工作更高效,注册即送10W Token,即刻开启你的AI之旅 广告
所属专题:[Python社区](README.md)   ## 问题 **输入:** 一个至多200个字符长度的字符串`$ s $`,和4个整数`$ a $`, `$ b $`, `$ c $`, `$ d $`. **输出:** 该字符串的两个子串,一个子串从下标`$ a $`到`$ b $`,另一个子串从下标`$ c $`到`$ d $`(两个子串下标范围均包含右端在内)。 **样例数据:** ``` HumptyDumptysatonawallHumptyDumptyhadagreatfallAlltheKingshorsesandalltheKingsmenCouldntputHumptyDumptyinhisplaceagain. 22 27 97 102 ``` **样例输出:** ``` Humpty Dumpty ```   ## 背景知识 该问题涉及Python语言的基本序列类型——字符串、列表的使用。详情请查阅ROSALIND网站上[关于该问题的背景说明](http://rosalind.info/problems/ini3/)。   ## 解答 ```python def slices(s, a, b): """在字符串s中取下标范围[a, b]的切片(包含b)""" sl = s[a:b+1] return sl ## --main-- l = [] with open("rosalind_ini3.txt", 'r') as f1: for line in f1.readlines(): line = line.strip('\n') l.append(line) s = l[0] pos = list(map(int, l[1].split())) result = str(slices(s, pos[0], pos[1])) + ' ' + str(slices(s, pos[2], pos[3])) with open("rosalind_ini3_out.txt", 'w') as f2: f2.write(result) ```