Capitalize every 3rd letter in a string - Python [closed]

左心房为你撑大大i 提交于 2020-05-09 16:57:26

问题


I'm just beginning to learn Python, so please bear with me. My math knowledge is a little shaky as well. I'm able to capitalize a single word, or first word in a string. What I am having a problem with is i need to capitalize every 3rd letter in the string. I need to do this as a function. I've used something like this, but can only get this to change the letter in one word, not every 3rd.

x = "string"
y = x[:3] + x[3].swapcase() + x[4:] 

There is a sample template that used

if i in phrase (len(phrase))

But i'm not sure how that works.

I'd like an output to show something like "thIs tExtIng fuNctIon"

Thanks in advance for any help.


回答1:


You can take a stride slice of an array which makes for a pretty and pythonic few lines:

s = "thisisareallylongstringwithlotsofletters"  

# convert to list
a = list(s)

#change every third letter in place with a list comprehension
a[2::3] = [x.upper() for x in a[2::3]]

#back to a string
s = ''.join(a)

# result: thIsiSarEalLylOngStrIngWitHloTsoFleTteRs

It's not clear what you want with spaces - this treats them like characters.




回答2:


Since you want every 3rd letter and not just the 3rd letter, we need to iterate the letters and generate the result according to the position of the character:

def cap_3rd(word):
    result = ""
    for i in range(1, len(word) + 1):
        if i % 3 == 0:
            result += word[i-1].upper()
        else:
            result += word[i-1]
    return result


word = "thisisaverylonglongword"
print(cap_3rd(word))  # thIsiSavEryLonGloNgwOrd



回答3:


x = "string"
z = list(x)
for x in range(2,len(z),3): # start from 3rd (index2) and step by 3  
  z[x] = z[x].upper()
x = ''.join(z)
print x

Output: StrIng




回答4:


If you don't care about letters and spaces:

''.join(phrase[i-1] if i % 3 or i == 0 else phrase[i-1].upper() for i in range(1, len(phrase) + 1))

If you only want to count letters:

new_phrase = ''
phrase = "here are some words"
counter = 0
for c in phrase:
    if not c.isalpha():
        new_phrase += c
    else:
        counter += 1
        if not counter % 3:
            new_phrase += c.upper()
        else:
            new_phrase += c

Since your example shows you using swapcase() instead of upper(), you can just replace upper() with swapcase() in this code to achieve that functionality if that's what you want.




回答5:


Try applying some split, and a lambda as below, and then join.

>>> x = "this texting function"
>>> " ".join(map(lambda w: w[:2] + w[2].swapcase() + w[3:], x.split()))
'thIs teXting fuNction'

If you are not a fan of lambda, then you can write a method like this

>>> def swapcase_3rd(string):
...     if len(string) >3:
...             return  string[:2] + string[2].swapcase() + string[3:]
...     if len(string) == 3:
...             return  string[:2] + string[2].swapcase()
...     return string
...
>>> x = "this texting function"
>>> " ".join(map(swapcase_3rd, x.split()))
'thIs teXting fuNction'


来源:https://stackoverflow.com/questions/45362458/capitalize-every-3rd-letter-in-a-string-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!