英文:
I need combine indexs of a tuple how can i do this
问题
我尝试将字母放大到单词的索引位置,但它不在相同的索引位置。
k = 'kars'
k = k[0].upper(), k1, k[2], k[3], k[4]
它在分布字母,如何将它们组合在一起?
英文:
I was trying a bigger letter to the index of word i did it but it is not in the same of index
k = 'kars'
k = k[0].upper(),k[1],k[2],k[3],k[4]
it is distributing the letters how can i combine them
答案1
得分: 2
Python有一个title()
方法:
k = 'kars'
print(k.title())
# 或者
print(k[0].upper() + k[1:])
# 或者
def uppercaseAtIndex(k, i):
newK = list(k)
newK[i] = newK[i].upper()
return ''.join(newK)
print(uppercaseAtIndex(k, 1))
输出:
Kars
Kars
kArs
英文:
Python has a title()
method:
k = 'kars'
print(k.title())
# OR
print(k[0].upper() + k[1:])
# OR
def uppercaseAtIndex(k, i):
newK = list(k)
newK[i] = newK[i].upper()
return ''.join(newK)
print(uppercaseAtIndex(k, 1))
Out:
Kars
Kars
kArs
答案2
得分: 1
''.join(letter.upper() if i == index else letter for i, letter in enumerate('thestring'))
英文:
Use a generator expression and enumerate to capitalize a specific index. Join the letters with str.join().
index = 1
''.join(letter.upper() if i==index else letter for i,letter in enumerate('thestring'))
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论