I have a string. I need to put a space before the first Russian character, and replace the first space with the underscore. Here is what I have:

s = 'come backвозвращаться' 

This is what I need:

 s = 'come_back возвращаться' 

Thank you!

    2 answers 2

    Maybe not the best option.

     import re s = s.replace(' ', '_', count=1) ind = s.index(re.findall('[А-яё]',s)[0]) s = '{} {}'.format(s[:ind], s[ind:]) 
    • one
      EMNIP, ё does not enter into the az. - andy.37
    • @ andy.37, really. Then, so re.findall('[А-яё]', 's') - mkkik
    • one
      s.replace() will replace all spaces with underscores (if there are more than one in the source line). We need to add the third parameter: s = s.replace(' ', '_', count=1) - insolor

    At the moment I cannot test my example, but there must be something like this:

     result = [] old = ' ' for ch in s: if ord != 32 and ord(ch) > 122 and 96 < ord(old) < 123: result.append(" " + ch) elif ord == 32: result.append("_") else: result.append(ch) old = ch print ''.join(result) 

    ASCII table

    There is one thing but in this implementation: a space will be added before any character that is not Latin or space. For more correct work, you need to look at ord from Cyrillic, it will most likely be different for different encodings, and accordingly change the first condition for these values.

    Perhaps someone will prompt the solution through regular expressions.