Akhiles Akhiles - 1 year ago 105
Python Question

Another capitalise every odd char of the string solution

I just started studying Python and created some kind of task for myself that I'm struggling to solve...

So, I'm on chapter on working with strings (accessing strings by index, changing etc).

My task is - capitalize every odd char of the string. I saw the solution here: Capitalise every other letter in a string in Python?

but I don't like it... wanted to solve this by slices. So what I produced is followig code:

T = 'somesampletexthere'

for i in range(0, len(T[1::2])):

print R

This code works great when there are even number of chars, for example with TESTTEXT but when the number is odd like here in EXAMPLE it will skipp last char (in this case E)

This is because the range is till
(length of even characters) but if I'll try the length of odd characters
I'll get error:

IndexError: string index out of range

logically because number of odd chars will always be bigger on 1 (in case of text with odd number of characters).

So, my question is - how can I supress the error? So Python just return null or something in case the index is out of range of the given string?

Or any other solutions of the task using slices?

Answer Source

Your are right to prefer a slicing solution over those in the linked question. (OTOH, that's a slightly different question because it skips spaces). However, your current code is rather inefficient because it recreates the T[::2] and T[1::2] slices on every iteration of the for loop. Also, calling .upper or .lower on single characters is less efficient than calling it on larger strings.

Here's an efficient way to do this using slicing.

T = 'somesampletexthere'
R = [''] * len(T)
R[::2], R[1::2] = T[::2].upper(), T[1::2].lower()
R = ''.join(R)



Just for fun, here's an alternative strategy that zips the upper & lower case strings together. We use izip_longest (or zip_longest in Python 3) so we can handle strings that have an odd length.

from itertools import izip_longest

T = 'somesampletexthere'
R = ''.join([c for t in izip_longest(T[::2].upper(), T[1::2].lower(), fillvalue='') for c in t])

Although this version does it in one line I prefer my first version: I find it more readable, and it's probably a little faster.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download