A.J. Uppal
A.J. Uppal

Reputation: 19264

Why is this not adding the correct string at the correct place?

I am looking for a way to do the following:

  1. Take in input as a string (with raw_input())
  2. Look for "strings" in the string (e.g. looking for 'hello' in ">>> x = 'hello'\n"
  3. Surrounding those strings with <font color="830000"> and </font> (e.g. ">>> x = 'hello'\n" becomes ">>> x = <font color="830000">'hello'</font>\n"

Here is my code:

string = raw_input('Enter a string: ')

count = 0

for k in range(0, len(string)):
    if string[k] == "'":
            count+=1
            if count % 2 == 0:
                    string = list(string)
                    string[k] = string[k]+'</font>'
                    string = ''.join(string)
            else:
                    string = list(string)
                    string[k] = '<font color="830000">'+string[k]
                    string = ''.join(string)

print string

This runs as:

$ python blaahh.py
Enter a string: >>> x = 'hello'\n>>> y = 'bye'\n
>>> x = <font color="830000">'</font>hello'\n>>> y = 'bye'\n

Desired output:

>>> x = <font color="830000">'hello'</font>\n>>> y = <font color="830000">'bye'</font>\n

Actual Output:

>>> x = <font color="830000">'</font>hello'\n>>> y = 'bye'\n

Why does it put my strings in the wrong place?

Upvotes: 0

Views: 55

Answers (2)

mblw
mblw

Reputation: 1798

As you said you need to look up "strings" in the input string and wrap it with prefix and suffix to do this the next function can be used:

def wrap_word(string, word):
    wrapped = "<font color=\"830000\">%s</font>" % word
    return wrapped.join(string.split(word))

string = raw_input("Enter string: ")
print wrap_word(string, "'hello'")

The result will be such that:

Enter string: >>> x = 'hello'\n>>> y = 'bye'\n
>>> x = <font color="830000">'hello'</font>\n>>> y = 'bye'\n

As for your output the result is:

>>> x = <font color="830000">'</font>hello'\n>>> y = 'bye'\n

Where 'hello' "string" wrapped totally wrong. So if you want to wrap one predefined "string" the wrap_word function can be used.

But as your example clarified the desire output is not to wrap the "string", the requirement is to wrap each word in the string which already wrapped by single quotes. Of course in this case regular expression is preferred how it was shown in @thefourtheye answer.

P.S. Sorry for initially wrong answer.

Upvotes: -1

thefourtheye
thefourtheye

Reputation: 239453

You can use Regular Expression to capture the string surrounded by 's and then add the HTML tags around them.

data = ">>> x = 'hello'\n>>> y = 'bye'\n"
import re
print re.sub(r"(\'.*?\')", r'<font color="830000">\1</font>', data)
# >>> x = <font color="830000">'hello'</font>
# >>> y = <font color="830000">'bye'</font>
# 

Here \1 represents the captured string, we replace that with the same string surrounded by html tags.

Note: Your code didn't work because, you are modifying the string while iterating over its indexes. So, length of the string varies when you change the string.

When the index is 8, you are adding <font color="830000">, so the actual string has changed and its length also has changed. So after adding the tag, the string becomes like this

>>> x = <font color="830000">'hello'\n>>> y = 'bye'\n

When the index becomes 29, it finds the ' before hello again, so it adds the closing tag after that.

Upvotes: 4

Related Questions