用Python写一段实现剪切功能的程序。

#!/usr/bin/env python
#coding: utf8

import sys

mystr = []

def inputstr():
    item = raw_input('Please input your string:')
    mystr[:] = []     #清空列表
    mystr.extend(item)  #将输入的字符串拆开为一个一个字符填入列表

def printstr():
    lenth = len(mystr) - 1
    index = 0
    print "Your result is :"
    print "*****" + ''.join(mystr) + "*****"
    #.join()与之前的extend对应,将字符合并为一个元素,用''里面的内容分割。''里面为空,则字符之间没有间隙
    print "----------------分割符----------------"

def leftstrip(): #左剪切
    while True:
        if mystr[0] == ' ':
            mystr.pop(0)
        else:
            break
    printstr()

def rightstrip():#右剪切
    while True:
        if mystr[-1] == ' ':
            mystr.pop()
        else:
            break
    printstr()

def bothsidestrip():
    while True:
        if mystr[-1] == ' ':
            mystr.pop()
        elif mystr[0] == ' ':
            mystr.pop(0)
        else:
            break
    printstr()
#使用字典的方式,实现case的语法功能
CMDs = {'l':leftstrip,'r':rightstrip,'b':bothsidestrip}

def showmenu():
    prompt = """(L)eftstrip
(R)ightstrip
(B)othsidestrip
(Q)uit
Please select a choice:"""
    while True:
        choice = raw_input(prompt).lower()
        if choice not in 'lrbq':
            continue
        if choice == 'q':
            break
        inputstr()
        CMDs[choice]()

if __name__=='__main__':
    showmenu()

结果截图

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值