1

I'm very new to Python and am trying to understand how to manipulate strings.

What I want to do is change a string by removing the spaces and alternating the case from upper to lower, IE "This is harder than I thought it would be" to "ThIsIsHaRdErThAnItHoUgHtItWoUlDbE"

I've cobbled together a code to remove the spaces (heavily borrowed from here):

string1 = input("Ask user for something.")
nospace = ""
for a in string1:
      if a == " ":
         pass
      else:
         nospace=nospace+a

... but just can't get my head around the caps/lower case part. There are several similar issues on this site and I've tried amending a few of them, with no joy. I realise I need to define a range and iterate through it, but that's where I draw a blank.

for c in nospace[::]:
    d = ""
    c = nospace[:1].lower()
    d = d + c
    c = nospace[:1].upper
print d

All I am getting is a column of V's. I'm obviously getting this very wrong. Please can someone advise where? Thanks in advance.

juanpa.arrivillaga
  • 65,257
  • 7
  • 88
  • 122

3 Answers3

2

Here is a cutesie way to do this:

>>> s = "This is harder than I thought it would be"
>>> from itertools import cycle
>>> funcs = cycle([str.upper, str.lower])
>>> ''.join(next(funcs)(c) for c in s if c != ' ')
'ThIsIsHaRdErThAnItHoUgHtItWoUlDbE'
>>>

Or, as suggested by Moses in the comments, you can use str.isspace, which will take care of not just a single space ' '

>>> ''.join(next(funcs)(c) for c in s if not c.isspace())
'ThIsIsHaRdErThAnItHoUgHtItWoUlDbE'

This approach only does a single pass on the string. Although, a two-pass method is likely performant enough.

Now, if you were starting with a nospace string, the best way is to convert to some mutable type (e.g. a list) and use slice-assignment notation. It's a little bit inefficient because it builds intermediate data structures, but slicing is fast in Python, so it may be quite performant. You have to ''.join at the end, to bring it back to a string:

>>> nospace
'ThisisharderthanIthoughtitwouldbe'
>>> nospace = list(nospace)
>>> nospace[0::2] = map(str.upper, nospace[0::2])
>>> nospace[1::2] = map(str.lower, nospace[1::2])
>>> ''.join(nospace)
'ThIsIsHaRdErThAnItHoUgHtItWoUlDbE'
>>>
juanpa.arrivillaga
  • 65,257
  • 7
  • 88
  • 122
0

So your 2nd loop is where you're breaking it, because the original list isn't being shortened, the c=nospace[:1] grabs the first character of the string and that's the only character that's ever printed. So a solution would be as follows.

string1 = str(input("Ask user for something."))

nospace = ''.join(string1.split(' '))

for i in range(0, len(nospace)):
    if i % 2 == 0:
        print(nospace[i].upper(), end="")
    else:
        print(nospace[i].lower(), end="")

Could also replace the if/else statement with a ternary opperator.

for i in range(0, len(nospace)):
    print(nospace[i].upper() if (i % 2 == 0) else nospace[i].lower(), end='')

Final way using enumerate as commented about

for i, c in enumerate(nospace):
    print(c.upper() if (i % 2 == 0) else c.lower(), end='')
Community
  • 1
  • 1
Alec C
  • 395
  • 1
  • 13
  • 1
    Instead of iterating over `range(0, len(something))` it is nicer to use `range(len(something))`. And if you are going to be using the index *and* the element, you actually just want enumerate: `for i, e in enumerate(something)` – juanpa.arrivillaga Mar 21 '17 at 22:42
  • I knew about the range thing, but since he said new to python thought I wouldn't skip that. However wasn't aware of the enumerate bit thanks! – Alec C Mar 21 '17 at 22:48
0

You're trying to do everything at once. Don't. Break your program into steps.

  1. Read the string.
  2. Remove the spaces from the string (as @A.Sherif just demonstrated here)
  3. Go over the string character by character. If the character is in an odd position, convert it to uppercase. Otherwise, convert to lowercase.
Community
  • 1
  • 1
zmbq
  • 35,452
  • 13
  • 80
  • 153
  • Nothing wrong with doing things in a single pass :) Especially if you take advantage of Python's beautiful, expressive and effective iterator constructs. – juanpa.arrivillaga Mar 21 '17 at 22:37