美文网首页
python实现语句中单词字符反转

python实现语句中单词字符反转

作者: 小云1121 | 来源:发表于2020-04-11 16:46 被阅读0次

题目描述:

The input is an English sentence as a string, we need a function - “function convert($input)” that can transform it as described below and return a new string.

The sentence would contain only alphabet (a-z and A-Z) and space, each word would be separated by exactly one space. There would be no spaces before and after the sentence.

Please return the string with each word spelled in reverse, however, the position of the capitalization of each letter should stay the same for each word

For example:.

Input: Many people spell MySQL incorrectly

Output: Ynam elpoep lleps LqSYM yltcerrocni

import string

def str_negative_sequence(str):

    a=str.split()

    c=str.split()

    print(a)

    for i in range(len(a)):

        a[i]=a[i][::-1]

        d=list(a[i])

        e=list(c[i])

        for j in range(len(e)):

            if e[j].isupper() is True:

                d[j]=d[j].upper()

            else:

                d[j]=d[j].lower()

        a[i]=''.join(d)           

        print(a[i])

    print(a)

    b=' '.join(a)

    return b

相关文章

网友评论

      本文标题:python实现语句中单词字符反转

      本文链接:https://www.haomeiwen.com/subject/mqrumhtx.html