【Python】【翻转字符串】

要求:在Python环境下用尽可能多的方法反转字符串,例如将s = "abcdef"反转成 "fedcba"
【1】:使用字符串切片
result = s[::-1]
【2】:使用列表的reverse方法

l = list(s)
l.reverse()
result = "".join(l)

【3】:使用reduce
result = reduce(lambda x,y:y+x,s)
【4】:使用递归函数

def fun(s):
    if len(s)<1:
            return s
    return func(s[1:])+[0]
result = func(s)

【5】:使用栈

def func(s):
    l = list(s) #模拟全部入栈
        result = ""
        while len(1)>0:
            result += 1.pop() #模拟出栈
        return result
result = func(s)

【6】:for循环

def func(s):
    result = ""
    max_index = len(s)-1
    for index,value in enumerate(s):
        result += s[max_index-index]
    return result
result = func(s)

原文地址:http://blog.51cto.com/12602176/2327777

时间: 2024-07-31 15:21:50

【Python】【翻转字符串】的相关文章

leetcode python翻转字符串里的单词

# Leetcode 151 翻转字符串里的单词### 题目描述给定一个字符串,逐个翻转字符串中的每个单词. **示例1:** 输入: "the sky is blue" 输出: "blue is sky the" **示例2:** 输入: " hello world! " 输出: "world! hello" 解释: 输入字符串可以在前面或者后面包含多余的空格,但是反转后的字符不能包括. **示例3:** 输入: "

Day 14 python 之 字符串练习

一.字符串总结与练习 1 #! /usr/bin/env python 2 # -*- coding: utf-8 -*- 3 # __author__ = "DaChao" 4 # Date: 2017/6/7 5 6 # x = "234567ASDsdfghj" #切片和索引 7 # print(x[2:-2]) 8 # print(x[2]) 9 10 # x = "hello" #显示字符串长度,注意是从1开始 11 # print(l

Python中字符串操作

#Python字符串操作 '''1.复制字符串''' #strcpy(sStr1,sStr2) sStr1 = 'strcpy' sStr2 = sStr1 sStr1 = 'strcpy2' print sStr2 '''2.连接字符串''' #strcat(sStr1,sStr2) sStr1 = 'strcat' sStr2 = 'append' sStr1 += sStr2 print sStr1 '''3.查找字符''' #strchr(sStr1,sStr2) sStr1 = 'st

lintcode 容易题:Reverse Words in a String 翻转字符串

题目: 翻转字符串 给定一个字符串,逐个翻转字符串中的每个单词. 样例 给出s = "the sky is blue",返回"blue is sky the" 说明 单词的构成:无空格字母构成一个单词 输入字符串是否包括前导或者尾随空格?可以包括,但是反转后的字符不能包括 如何处理两个单词间的多个空格?在反转字符串中间空格减少到只含一个 解题: 这个题目方法很多的 1.整体反转,对每个单词再反转,但是中间的多个空格还有单独处理 2.把后面的单词,拿到前面去.参考程序

python的字符串详解

python的字符串内建函数详解: 一.string.capitalize() 描述: string.capitalize():将字符串的第一个字母变成大写,其他字母变小写.对于 8 位字节编码需要根据本地环境. 语法:          name = 'luwenjuan'          name.capitalize() 无参数 二.string.decode(encoding='UTF-8', errors='strict') 描述: string.decode(encoding='U

6 Python 数据类型—字符串

字符串是 Python 中最常用的数据类型.我们可以使用引号('或")来创建字符串. 创建字符串很简单,只要为变量分配一个值即可. var1 = 'Hello World!' var2 = "Python Runoob" Python访问字符串中的值 Python不支持单字符类型,单字符也在Python也是作为一个字符串使用. Python访问子字符串,可以使用方括号来截取字符串 1 var1 = 'Hello World!' 2 var2 = "Python Ru

Python 常用字符串操作

Python 字符串操作(string替换.删除.截取.复制.连接.比较.查找.包含.大小写转换.分割等) 去空格及特殊符号 s.strip() .lstrip() .rstrip(',') 复制字符串 #strcpy(sStr1,sStr) sStr= 'strcpy' sStr = sStr sStr= 'strcpy' print sStr 连接字符串 #strcat(sStr1,sStr) sStr= 'strcat' sStr = 'append' sStr+= sStr print

[LeetCode] 151. Reverse Words in a String 翻转字符串中的单词

Given an input string, reverse the string word by word. For example,Given s = "the sky is blue",return "blue is sky the". Update (2015-02-12):For C programmers: Try to solve it in-place in O(1) space. Clarification: What constitutes a

python的字符串一些方法

python的字符串内建函数 字符串方法是从python1.6到2.0慢慢加进来的——它们也被加到了Jython中. 这些方法实现了string模块的大部分方法,如下表所示列出了目前字符串内建支持的方法,所有的方法都包含了对Unicode的支持,有一些甚至是专门用于Unicode的. 方法 描述 string.capitalize() 把字符串的第一个字符大写 string.center(width) 返回一个原字符串居中,并使用空格填充至长度 width 的新字符串 string.count(

Reverse Words in a String(翻转字符串)

给定一个字符串,逐个翻转字符串中的每个单词. Given s = "the sky is blue",return "blue is sky the". 单词的构成:无空格字母构成一个单词 输入字符串是否包括前导或者尾随空格?可以包括,但是反转后的字符不能包括 如何处理两个单词间的多个空格?在反转字符串中间空格减少到只含一个 思路: 1.定义一个新的字符串str 接收原字符串删除首尾空字符后的字符串 trim(): 2.定义字符串tmp 存储遍历字符串时的每个单词: