Python给定一个句子倒序输出单词以及字母的方法

yipeiwu_com7年前Python基础

如下所示:

#!/usr/bin/python
# -*- coding: utf-8 -*-
def rever(sentence):
  newwords = []
  words = sentence.split()
  words.reverse()
  space = ' '#单词之间一个间隔
  for word in words:
    newword = []
    new = ''#单词的字母间无间隔
    l = len(word)
    for i in range(l):
      newword.append(word[l-i-1])
    neww = new.join(newword)
    newwords.append(neww)
  print space.join(newwords)
rever('what\'s your problem man ?')

运行结果:

? nam melborp ruoy s'tahw

以上这篇Python给定一个句子倒序输出单词以及字母的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

Python实现判断一行代码是否为注释的方法

目前的编辑器大都可以自动检测某一行代码是否为代码行或注释行,但并不太提供代码行/注释行行数的统计,对于大量代码文件的代码行/注释行统计,就更少见一些。本篇文章试用一段Python脚本来实...

python、PyTorch图像读取与numpy转换实例

Tensor转为numpy np.array(Tensor) numpy转换为Tensor torch.Tensor(numpy.darray) PIL.Image.Image转换成nu...

Python中遍历字典过程中更改元素导致异常的解决方法

先来回顾一下Python中遍历字典的一些基本方法: 脚本: #!/usr/bin/python dict={"a":"apple","b":"banana","o":"orange...

python判断字符串是否包含子字符串的方法

本文实例讲述了python判断字符串是否包含子字符串的方法。分享给大家供大家参考。具体如下: python的string对象没有contains方法,不用使用string.contain...

Python采用socket模拟TCP通讯的实现方法

本文实例讲述了Python采用socket模拟TCP通讯的实现方法。分享给大家供大家参考。具体实现方法如下: 对于TCP server端的创建而言,分为如下几个步骤: 创建socket对...