简体   繁体   English

Python 如何将字符串的第n个字母大写

[英]Python How to capitalize nth letter of a string

I tried this: Capitalize a string .我试过这个: 大写字符串 Can anybody provide a simple script/snippet for guideline?任何人都可以提供一个简单的脚本/片段作为指导吗?

Python documentation has capitalize() function which makes first letter capital. Python 文档有capitalize() function 使首字母大写。 I want something like make_nth_letter_cap(str, n) .我想要类似make_nth_letter_cap(str, n)的东西。

Capitalize n-th character and lowercase the rest as capitalize() does:大写第 n 个字符并将其余字符小写,就像capitalize()所做的那样:

def capitalize_nth(s, n):
    return s[:n].lower() + s[n:].capitalize()
my_string[:n] + my_string[n].upper() + my_string[n + 1:]

或者更有效的版本不是Schlemiel 画家的算法

''.join([my_string[:n], my_string[n].upper(), my_string[n + 1:]])

This is the comprehensive solution: either you input a single word, a single line sentence or a multi line sentence, the nth letter will be converted to Capital letter and you will get back the converted string as output:这是综合解决方案:输入单个单词、单行句子或多行句子,第n个字母将转换为大写字母,您将返回转换后的字符串为output:

You can use this code:您可以使用以下代码:

def nth_letter_uppercase(string,n):
  
  listofwords = string.split()
  sentence_upper = ''

  for word in listofwords:
  
    length = len(word)
      
    if length > (n - 1):
      new_word = word[:n-1] + word[n-1].upper() + word[n:]
      
    else:
      new_word = word
          
    sentence_upper += ' ' + new_word

  return sentence_upper

calling the function defined above (I want to convert 2nd letter of each word to a capital letter):调用上面定义的 function (我想将每个单词的第二个字母转换为大写字母):

string = '''nature is beautiful
and i love python'''
nth_letter_uppercase(string,2)

output will be: output 将是:

'nAture iS bEautiful aNd i lOve pYthon'
x = "string"
y = x[:3] + x[3].swapcase() + x[4:]  

Output输出

strIng  

Code代码

Keep in mind that swapcase will invert the case whether it is lower or upper.请记住, swapcase将反转大小写,无论它是较低还是较高。
I used this just to show an alternate way.我用这个只是为了展示另一种方式。

I know it's an old topic but this might be useful to someone in the future:我知道这是一个古老的话题,但这可能对将来的某人有用:

def myfunc(str, nth):
new_str = '' #empty string to hold new modified string
for i,l in enumerate(str): # enumerate returns both, index numbers and objects
    if i % nth == 0: # if index number % nth == 0 (even number)
        new_str += l.upper() # add an upper cased letter to the new_str
    else: # if index number nth
        new_str += l # add the other letters to new_str as they are
return new_str # returns the string new_str

A simplified answer would be:一个简化的答案是:

    def make_nth_letter_capital(word, n):
        return word[:n].capitalize() + word[n:].capitalize()
def capitalize_n(string, n):
return string[:n] + string[n].capitalize() + string[n+1:]

This works perfect这工作完美

You can use:您可以使用:

def capitalize_nth(text, pos):
    before_nth = text[:pos]
    n = text[pos].upper()
    new_pos = pos+1
    after_nth = text[new_pos:]
    word = before_nth + n + after_nth
    print(word)

capitalize_nth('McDonalds', 6)

The outcome is:结果是:

'McDonaLds'

I think this is the simplest among every answer up there...我认为这是所有答案中最简单的......

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM