给定一个字符串,你需要反转字符串中每个单词的字符顺序,同时仍保留空格和单词的初始顺序。
示例:
输入:"Let's take LeetCode contest"
输出:"s'teL ekat edoCteeL tsetnoc"
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/reverse-words-in-a-string-iii
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
思路
" ".join(word for word in s.split(" "))
输出"Let's take LeetCode contest"(正序
class Solution(object): def reverseWords(self, s): """ :type s: str :rtype: str """ return " ".join(word[::-1] for word in s.split(" "))
切片
知识点:
https://www.runoob.com/python3/python3-string-join.html
https://www.runoob.com/python/att-string-split.html
https://www.runoob.com/note/51257