1. 程式人生 > 程式設計 >基於python3實現倒敘字串

基於python3實現倒敘字串

這篇文章主要介紹了基於python3實現倒敘字串,文中通過示例程式碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下

google測試工程師的一道題:

設計一個函式,使用任意語言,完成以下功能:

一個句子,將句子中的單詞全部倒排過來,但單詞的字母順序不變。比如,This is a real world,輸出結果為

world real a is this.

下面利用python來實現:

句子為:

基於python3實現倒敘字串

程式碼如下

#!/usr/bin/env python3.4
# -*- coding: utf-8 -*-

#某一段文字
data = "You don't need us to tell you that China's Internet space is booming. With the world's largest Internet user population—686 million as of January 2016—and a long way to go to reach Internet penetration levels of developed countries,China's Internet industry is growing in both scale and influence. And as more and more Chinese users come online,Baidu continues to innovate to meet their changing needs and diverse tastes. We aim to serve the needs of our users and customers with products and solutions that prioritize the user experience and reflect our corporate culture – simple and reliable."

#按照空格分割
strings = data.split()
arr = []

#打印出來
for string in strings:
  arr.append(string)

#將文字倒敘
arr.reverse()
# 按照空格將文字變為字串
newstring = " ".join(arr)

print(newstring)

結果:

基於python3實現倒敘字串

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援我們。