Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/338.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何将Python字符串中每个单词的首字母和末字母大写?_Python - Fatal编程技术网

如何将Python字符串中每个单词的首字母和末字母大写?

如何将Python字符串中每个单词的首字母和末字母大写?,python,Python,我想编写一个python函数,这样字符串中单词的首字母和末字母就可以大写。字符串包含小写字母和空格。 我想做一些类似的事情: def capitalize(s): s.title() s[len(s) - 1].upper() return s 但这似乎不起作用。有什么建议吗 例如,字符串“我喜欢猫”应该变成“我喜欢猫”尝试使用切片 def upup(s): if len(s) < 2: return s.upper() re

我想编写一个python函数,这样字符串中单词的首字母和末字母就可以大写。字符串包含小写字母和空格。 我想做一些类似的事情:

def capitalize(s):
     s.title()
     s[len(s) - 1].upper()
     return s
但这似乎不起作用。有什么建议吗

例如,字符串“我喜欢猫”应该变成“我喜欢猫”

尝试使用切片

def upup(s):
    if len(s) < 2:
        return s.upper()
    return ''.join((s[0:-1].title(),s[-1].upper())))
这是一条漂亮的单行线。(适用于高尔夫球手:P)

它演示了当输入为0或1个字符时解决问题的另一种方法

它可以很容易地应用于字符串以大写单个单词:

' '.join(map(capEnds, 'I like cats'.split(' '))) 
'I LikE CatS'
输出

I LikE CatS 

title()
应用于整个字符串,然后对字符串中的每个单词将最后一个字符大写,并将它们重新附加在一起。

下面是我们要做的:

  • 把句子分成几个词
  • 应用一个将单词的第一个和最后一个单词大写的函数
  • 重新组合句子
  • 将函数写入(2)
  • 因此:

    0)

    words=“欢迎来到丛林!”

    (一)

    (二)

    (三)

    (四)


    我对一条有趣的单行线的看法:

    def cap_both(phrase):
        return ' '.join(map(lambda s: s[:-1]+s[-1].upper(), phrase.title().split()))
    
    演示:


    试试这段简单易懂的代码

    st = 'this is a test string'
    
    def Capitalize(st):    
        for word in st.split():
            newstring = ''
            if len(word) > 1:
                word = word[0].upper() + word[1:-1] + word[-1].upper()
            else:
                word = word[0].upper()
            newstring += word
            print(word)
    
    然后调用下面的函数

    Capitalize(st)
    

    一篇非常古老的文章,但另一个有趣的使用列表理解的一行:

    cap = lambda st: (" ").join([x.title().replace(x[-1], x[-1].upper()) for x in st.split()])
    
    >>> cap("I like cats")
    'I LikE CatS'
    

    Python字符串是不可变的。您必须重新分配它:
    s=s.title()
    @edgararroutiounian
    如何开始使用
    结束使用
    帮助?虽然在大多数情况下会这样做,但这不太管用。。(提示;^)@DSM我不懂,除非你说的是0长度的字符串?@DSM哈哈,好吧,你明白了。已编辑。由于字符串是不可变的,因此使用缓冲区或“”。联接(列表)将拆分为多个部分的字符串联接起来肯定会更快。在这种情况下,只有三个部分,我不确定
    ''。加入
    对你有什么好处。很好!但是,如何删除字符串后面的最后一个空格?@perry请检查答案。我们可以做<代码>返回结果[]:-1)<代码>,而不是<代码>返回结果< /代码>以去掉最后一个空间。@ PrRy请考虑投票并接受这个答案,如果它有助于@ JTARCE它是否工作取决于你如何看待OP所问的两个不同的问题。他的函数和标题都表示他想将字符串的首字母和末字母大写。他的示例(他后来才添加)表明他希望将字符串中每个单词的首字母和末字母大写。您的
    capitalize
    函数无法处理一个字母的单词。
    >>> words = [capitalize(x) for x in words]
    
    >>> words = " ".join(words)
    
    def capitalize(word):
        return word[0].capitalize() + word[1:-1] + word[-1].capitalize()
    
    def cap_both(phrase):
        return ' '.join(map(lambda s: s[:-1]+s[-1].upper(), phrase.title().split()))
    
    >>> cap_both('i like cats')
    'I LikE CatS'
    >>> cap_both('a')
    'A'
    
    st = 'this is a test string'
    
    def Capitalize(st):    
        for word in st.split():
            newstring = ''
            if len(word) > 1:
                word = word[0].upper() + word[1:-1] + word[-1].upper()
            else:
                word = word[0].upper()
            newstring += word
            print(word)
    
    Capitalize(st)
    
    cap = lambda st: (" ").join([x.title().replace(x[-1], x[-1].upper()) for x in st.split()])
    
    >>> cap("I like cats")
    'I LikE CatS'