python实现剪切功能

yipeiwu_com6年前Python基础

本文实例为大家分享了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()

效果图:

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

利用python实现简单的循环购物车功能示例代码

本文主要给大家介绍了关于python实现循环购物车功能的相关内容,分享出来供大家参考学习,下面来一起看看详细的介绍: 示例代码 # -*- coding: utf-8 -*- __a...

python实现推箱子游戏

python实现推箱子游戏

本文实例为大家分享了python实现推箱子游戏的具体代码,供大家参考,具体内容如下 题目描述: 最短路径为: uurrDDDDuuuulldRurDDDrddLLrruLuuulldR...

关于Python中Inf与Nan的判断问题详解

大家都知道 在Python 中可以用如下方式表示正负无穷: float("inf") # 正无穷 float("-inf") # 负无穷 利用 inf(infinite) 乘以 0...

python访问mysql数据库的实现方法(2则示例)

本文实例讲述了python访问mysql数据库的实现方法。分享给大家供大家参考,具体如下: 首先安装与Python版本匹配的MySQLdb 示例一 import MySQLdb co...

Django框架orM与自定义SQL语句混合事务控制操作

本文实例讲述了Django框架orM与自定义SQL语句混合事务控制操作。分享给大家供大家参考,具体如下: 用单纯的ORM 或者单纯的自定义SQL语句,都比较好控制事务。在前面的一篇文章中...