2019-02-28Python中移动函数
自定义函数move_substr(s, flag, n),将传入的字符串s按照flag(1代表循环左移,2代表循环右移)的要求左移或右移n位(例如对于字符串abcde12345,循环左移两位后的结果为cde12345ab,循环右移两位后的结果为45abcde123),结果返回移动后的字符串,若n超过字符串长度则结果返回-1
代码如下:
def moveSubstr(s,flag,n): if n>len(s): return -1 else: if flag==1: return s[n:]+s[:n] else: return s[-n:]+s[:-n] s,flag,n=input("enter the 'string,flag,n':").split(',') result =moveSubstr(s,int(flag),int(n)) if result !=-1: print(result) else: print('n is too big')
相关文章
- Python报错:SyntaxError: Missing parentheses in call to 'print'
- 一篇文章入门Python
- Python入门经典编程题2
- Python中移动函数
- anaconda安装好了只有anaconda prompt,而没有Spyder,Anaconda Navigator等等
- Python如何在一个文本的不同行前加入序号?
- Python中with open语句写入文件时显示“io.UnsupportedOperation: not writable”
- Python入门经典编程题1
- Python 中 'unicodeescape' codec can't decode bytes in position XXX: trun错误解决方案
- Python 3.6.0 Shell中print时候出现SyntaxError: invalid syntax解决方法
暂无留言,快抢沙发!