在Python中重写大小写字符串

在Python中重写大小写字符串,python,Python,我对Phyton很陌生,所以我要你帮忙。 关于字符串,可以使用s=s.upper编写大写字符,使用s=s.lower编写小写字符 但是,如何重写给定的字符串,使前五个字符为大写,后五个字符为小写,依此类推,最后字符串长度以10个字符的宽度写入?在python 2.7中尝试此方法 from __future__ import print_function s='stackoverflow' for i in range(0,len(s),5): if (int(i%2) == 0):

我对Phyton很陌生,所以我要你帮忙。 关于字符串,可以使用s=s.upper编写大写字符,使用s=s.lower编写小写字符

但是,如何重写给定的字符串,使前五个字符为大写,后五个字符为小写,依此类推,最后字符串长度以10个字符的宽度写入?

在python 2.7中尝试此方法

from __future__ import print_function
s='stackoverflow'
for i in range(0,len(s),5):
      if (int(i%2) == 0):
            print ( s[i:i+5].lower() ,end = "")
      else:
            print (s[i:i+5].upper(),end = "")
#If you want 10 spaces before displaying lenght
print (" "*10 ,end = " ")
print (len(s))

#if you want to reserve 10digits for displaying length replace last two lines with below  line
print ("%10d" %(len(s)))

这里有一个简单的方法来获取字符串,将它的前5个字母改为大写,并将后面的所有字母改为小写。您不必为此导入任何库

string = "abcdefghij"

string1 = string[:5]

string2 = string[5:]

stringfinal = string1.upper() + string2.lower()

print(stringfinal)
print(len(stringfinal))

有很多方法可以做到这一点。你试过什么了吗?StackOverflow上的用户会在你自助之后帮助你。参加旅行:。做一些研究:。了解如何发布好问题:。和.嗨..如果字符串是15个字符呢?你在哪里添加OP要求的空间?@FabianVit:很高兴听到这个消息。如果答案有帮助,请按照更新线程