Home > database >  Modify characters between two symbols in a string
Modify characters between two symbols in a string

Time:10-25

Here is my problem, I have to write a function that receives a string and returns that string with the characters between "*" in uppercase, for example given that string: “I want *this text* to be uppercase”, it returns : “I want THIS TEXT to be uppercase”. Here is the code that I have written:

l = []
def func(s):
    inside = False
    for i in s:
        if i == "*" and not inside:
            inside = True
            while inside:
                if i == "*":
                    inside = False
                else:
                    i.upper()
    l.append(i)
            
    print(s)
                

When I run the program it prints out the text without any change. What am I doing wrong? Thanks

CodePudding user response:

I think you've tried to make this more complicated than it is. You need to find the index of both asterisks. From that you can get three slices of the original string and apply upper() to the slice between the asterisks. Note that this code will fail if there are fewer than two asterisks in the string.

def dotricks(s):
    i = s.index('*')
    j = s.index('*', i i)
    return s[0:i]   s[i 1:j].upper()   s[j 1:]

print(dotricks('I want *this text* to be uppercase'))

CodePudding user response:

You are not changing the string in your code. In this edit below, I've assigned the letters of the string to a new variable. And used continue to skip over the "*". Also, at the end your append will give you a list of letters which you need to use .join() to concatentate.

Try this edit to your code, tested and working:

l = []
def func(s):
    inside = False
    temp = ""
    for i in s:
        if i == "*" and not inside:
            inside = True
            continue
        if inside:
            if i == "*":
                inside = False
                continue
            else:
                temp = i.upper()
        else:
            temp = i
        l.append(temp)
            
    new_string = "".join(l)
    print(new_string)
    return new_string

func("I want *this text* to be uppercase")

CodePudding user response:

There are several issues here:

  1. You don't return anything inside your function
  2. Your while loop is pointless as i is not incremented inside of it
  3. You need to assign i to i.upper()
  4. You need to convey the input to a list

The corrected code would be as follows:

l = []
def func(s):
    inside = False
    for i in s:
        if i == "*" and not inside:
            inside = True
            while inside:
                if i == "*":
                    inside = False
                else:
                    i.upper()
        if i != "*":
            l.append(i)
    return l

CodePudding user response:

I would leverage the power of the re module:

import re

st = "I want *this text* to be uppercase and *this one*"

v = re.findall("\*(.*?)\*", st)

for s in v:
    st = st.replace(f'*{s}*', s.upper())

print(st)

Output:

>>> I want THIS TEXT to be uppercase and THIS ONE

Anyway, re-editing your code:

def func(s):
    l = []
    inside = False
    for i in s:
        if i == "*": 
            inside = not inside  # switch inside on/off when * if found

        if inside:
            i = i.upper() # upper the character if inside == True
        l.append(i)
    return l

If you look at your original code, part of the problem is in the following logic:

if i == "*" and not inside:
    inside = True  # inside is set to True when * is found....
        while inside:
            if i == "*":
                inside = False # and immediately set to False again!
  • Related