0

I have string that looks like this : 'Foo dooo kupa trooo bar'.

I know the start and end point of word kupa and I need to wrap it with this : <span> </span>.

After this operation i want my string to like like this : Foo dooo <span>kupa</span> trooo bar

I cannot find any good built-in methods that can help so any help would be nice.

Cosaquee
  • 724
  • 1
  • 6
  • 22

2 Answers2

0

basically there are two options:

import re
strg = 'Foo dooo kupa trooo bar'

match = re.search('kupa', strg)
print('{}<span>{}</span>{}'.format(strg[:match.start()], strg[match.start():match.end()], strg[match.end():]))

or (the first expressioin would be a regex, if the pattern were a little more complicated):

print(re.sub('kupa', '<span>kupa</span>', strg))
hiro protagonist
  • 44,693
  • 14
  • 86
  • 111
0

The output string and the input string are the same.....

Anyway I think you want to replace part of your string, so have a look at this answers:

  1. https://stackoverflow.com/a/10037749/4827890
  2. https://stackoverflow.com/a/12723785/4827890
Community
  • 1
  • 1
David P
  • 1,023
  • 10
  • 17