Python 为什么不在正确的位置添加正确的字符串?

Python 为什么不在正确的位置添加正确的字符串?,python,string,python-2.7,Python,String,Python 2.7,我正在寻找一种方法来实现以下目标: 以字符串形式接收输入(使用raw\u input()) 在字符串中查找“字符串”(例如,在“>>>中查找'hello'x='hello'\n“ 用和包围这些字符串(例如“>>>x='hello'\n”将变成“>>x='hello'\n” 这是我的密码: string = raw_input('Enter a string: ') count = 0 for k in range(0, len(string)): if string[k] == "'

我正在寻找一种方法来实现以下目标:

  • 以字符串形式接收输入(使用
    raw\u input()
  • 在字符串中查找“字符串”(例如,在
    “>>>中查找
    'hello'
    x='hello'\n“
  • 包围这些字符串(例如
    “>>>x='hello'\n”
    将变成
    “>>x='hello'\n”
  • 这是我的密码:

    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
    
    string=raw_输入('输入字符串:')
    计数=0
    对于范围(0,len(string))中的k:
    如果字符串[k]==“'”:
    计数+=1
    如果计数%2==0:
    字符串=列表(字符串)
    字符串[k]=字符串[k]+''
    字符串=“”。连接(字符串)
    其他:
    字符串=列表(字符串)
    字符串[k]=''+字符串[k]
    字符串=“”。连接(字符串)
    打印字符串
    
    其运行方式如下:

    $ python blaahh.py
    Enter a string: >>> x = 'hello'\n>>> y = 'bye'\n
    >>> x = <font color="830000">'</font>hello'\n>>> y = 'bye'\n
    
    $python blaahh.py
    输入字符串:>>>x='hello'\n>>>y='bye'\n
    >>>x='你好'\n>>>y='再见'\n
    
    期望输出:

    >>> x = <font color="830000">'hello'</font>\n>>> y = <font color="830000">'bye'</font>\n
    
    >>x='hello'\n>>>y='bye'\n
    
    实际产量:

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

    为什么它会将我的字符串放在错误的位置?

    您可以使用正则表达式捕获由
    '包围的字符串,然后在其周围添加HTML标记

    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>
    # 
    

    当索引变为29时,它会再次在
    hello
    之前找到
    ,因此它会在之后添加结束标记。

    您可以使用正则表达式捕获由
    '
    包围的字符串,然后在其周围添加HTML标记

    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>
    # 
    

    当索引变为29时,它会再次在
    hello
    之前找到
    ,因此它会在之后添加结束标记。

    您可以使用正则表达式捕获由
    '
    包围的字符串,然后在其周围添加HTML标记

    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>
    # 
    

    当索引变为29时,它会再次在
    hello
    之前找到
    ,因此它会在之后添加结束标记。

    您可以使用正则表达式捕获由
    '
    包围的字符串,然后在其周围添加HTML标记

    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>
    # 
    

    当索引变为29时,它会再次在
    hello
    之前找到
    ,因此它会在之后添加结束标记。

    正如您所说,您需要在输入字符串中查找“字符串”,并用前缀和后缀将其包装。要执行此操作,可以使用下一个函数:

    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'")
    
    def wrap_单词(字符串,单词):
    wrapped=“%s”%word
    返回wrapped.join(string.split(word))
    字符串=原始输入(“输入字符串:”)
    打印换行字(字符串“'hello'”)
    
    结果将是:

    Enter string: >>> x = 'hello'\n>>> y = 'bye'\n
    >>> x = <font color="830000">'hello'</font>\n>>> y = 'bye'\n
    
    输入字符串:>>>x='hello'\n>>>y='bye'\n
    >>>x='你好'\n>>>y='再见'\n
    
    至于您的输出,结果是:

    >>> x = <font color="830000">'</font>hello'\n>>> y = 'bye'\n
    
    >>x='hello'\n>>>y='bye'\n
    
    其中“hello”的“string”包装完全错误。因此,如果要包装一个预定义的“string”,可以使用
    wrap\u word
    函数

    但是,正如您的示例所阐明的,期望的输出不是包装“字符串”,而是将每个单词包装在已经用单引号包装的字符串中。当然,在本例中,正则表达式是首选的,它在@thefourtheye答案中的显示方式


    请注意,很抱歉最初回答错误。

    正如您所说,您需要在输入字符串中查找“字符串”,并用前缀和后缀将其包装起来。要执行此操作,可以使用下一个函数:

    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'")
    
    def wrap_单词(字符串,单词):
    wrapped=“%s”%word
    返回wrapped.join(string.split(word))
    字符串=原始输入(“输入字符串:”)
    打印换行字(字符串“'hello'”)
    
    结果将是:

    Enter string: >>> x = 'hello'\n>>> y = 'bye'\n
    >>> x = <font color="830000">'hello'</font>\n>>> y = 'bye'\n
    
    输入字符串:>>>x='hello'\n>>>y='bye'\n
    >>>x='你好'\n>>>y='再见'\n
    
    至于您的输出,结果是:

    >>> x = <font color="830000">'</font>hello'\n>>> y = 'bye'\n
    
    >>x='hello'\n>>>y='bye'\n
    
    其中“hello”的“string”包装完全错误。因此,如果要包装一个预定义的“string”,可以使用
    wrap\u word
    函数

    但是,正如您的示例所阐明的,期望的输出不是包装“字符串”,而是将每个单词包装在已经用单引号包装的字符串中。当然,在本例中,正则表达式是首选的,它在@thefourtheye答案中的显示方式


    请注意,很抱歉最初回答错误。

    正如您所说,您需要在输入字符串中查找“字符串”,并用前缀和后缀将其包装起来。要执行此操作,可以使用下一个函数:

    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'")
    
    def wrap_单词(字符串,单词):
    wrapped=“%s”%word
    返回wrapped.join(string.split(word))
    字符串=原始输入(“输入字符串:”)
    打印换行字(字符串“'hello'”)
    
    结果将是:

    Enter string: >>> x = 'hello'\n>>> y = 'bye'\n
    >>> x = <font color="830000">'hello'</font>\n>>> y = 'bye'\n
    
    输入字符串:>>>x='hello'\n>>>y='bye'\n
    >>>x='你好'\n>>>y='再见'\n
    
    至于您的输出,结果是:

    >>> x = <font color="830000">'</font>hello'\n>>> y = 'bye'\n
    
    >>x='hello'\n>>>y='bye'\n
    
    其中“hello”的“string”包装完全错误。因此,如果要包装一个预定义的“string”,可以使用
    wrap\u word
    函数

    但是,正如您的示例所阐明的,期望的输出不是包装“字符串”,而是将每个单词包装在已经用单引号包装的字符串中。当然,在本例中,正则表达式是首选的,它在@thefourtheye答案中的显示方式


    请注意,很抱歉最初回答错误。

    正如您所说,您需要在输入字符串中查找“字符串”,并用前缀和后缀将其包装起来。要执行此操作,可以使用下一个函数:

    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'")
    
    def wrap_单词(字符串,单词):
    wrapped=“%s”%word
    返回wrapped.join(string.split(word))
    字符串=原始输入(“输入字符串:”)
    打印换行字(字符串“'hello'”)
    
    结果将是:

    Enter string: >>> x = 'hello'\n>>> y = 'bye'\n
    >>> x = <font color="830000">'hello'</font>\n>>> y = 'bye'\n
    
    输入字符串:>>>x='hello'\n>>>y='bye'\n
    >>>x='你好'\n>>>y='再见'\n
    
    至于您的输出,结果是:

    >>> x = <font color="830000">'</font>hello'\n>>> y = 'bye'\n
    
    >>x='hello'\n>>>y='bye'\n
    
    其中“hello”的“string”包装完全错误。所以如果你想包装一个pred