How do we print a string, changing characters to a repeating pattern of lower case, upper case, asterisk?

Question:

Started with:

instring= input('Enter a string...')
print('The string is...', instring)

The question I’m doing needs me to modify this to print the string, but changing characters to a repeating pattern of lower case, upper case, and asterisk. So e.g. helloHELLO becomes hE*lO*eL*o.

After looking around a bit on the internet I managed to get the upper and lower case part:

instring = input('Enter a string...')
res =""
for i in range(len(instring)):
   if not i % 2:
      res = res + instring[i].upper()
   else:
      res = res + instring[i].lower()

print( "The string is: " +  str(res))

However, I’m still unsure how to get it to replace parts with an *.

I’m a complete beginner at coding so I apologise if this is a stupid question or if I haven’t asked the question on here the correct way.

Asked By: Keenan Enrilsen

||

Answers:

With s = 'helloHELLO':

print(''.join(
    (c.lower(), c.upper(), '*')[i % 3]
    for i, c in enumerate(s)
))

or

a = ['*'] * len(s)
a[0::3] = s[0::3].lower()
a[1::3] = s[1::3].upper()
print(''.join(a))

or

it = iter(s)
print(''.join(
    c.lower() + next(it, '').upper() + (next(it, '') and '*')
    for c in it
))

Attempt This Online!

Answered By: Kelly Bundy

So from your example I believe that you mean to change every first character to lower case, every second character to upper case and every third to an asterisk.

One way you can do this is to loop through the array and when i % 3 == 0, let c = c.lower(), when i % 3 == 1, let c = c.upper() and when i % 3 == 2, let c = ‘*’
like this

instring = input('Enter a string...')
res = ""
for i in range(len(instring)):
    if i % 3 == 0:
        res = res + instring[i].lower()
        # you can also write res += instring[i].lower()
    if i % 3 == 1:
    # you can also use elif i % 3 == 1: here as both branches cannot be true 
    # simultanesously
        res = res + instring[i].upper()
    if i % 3 == 2:
    # likewise, else: can be used here
        res = res + '*'

print( "The string is: " +  str(res))
Answered By: yxz

Since this seems to be homework, I’m going to provide incomplete code and let you fill in the gaps.


You know how to use modulo to check if a number is divisible by two, now you just need to use it to check divisibility by three:

  • for a multiple of three, the remainder is zero
  • for a multiple of three plus one, the remainder is one
  • and for a multiple of three plus two, the remainder is two, but we can just use else for that case.

Secondly, two things that are beside the point, but good practice:

  • Avoid for i in range(len(x)) ... x[i], because enumerate is cleaner.
  • Avoid building a string in a loop; use a list instead, append to it in the loop, then join.
instring = 'helloHELLO'  # for testing

out = []
for i, c in enumerate(instring):
    if i % 3 == 0:
        # do something with c
    elif i % 3 == 1:
        # do something else with c
    else:
        # do something with "*"

res = ''.join(out)
Answered By: wjandrea
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.