我需要合并元组的索引,我该怎么做?

huangapple go评论60阅读模式
英文:

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'))

huangapple
  • 本文由 发表于 2023年2月7日 03:21:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/75365691.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定