Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7530189
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T04:50:35+00:00 2026-05-30T04:50:35+00:00

I need to highlight the differences between two simple strings with python, enclosing the

  • 0

I need to highlight the differences between two simple strings with python, enclosing the differing substrings in a HTML span attribute. So I’m looking for a simple way to implement the function illustrated by the following example:

hightlight_diff('Hello world','HeXXo world','red')

…it should return the string:

'He<span style="color:red">XX</span>o world'

I have googled and seen difflib mentioned, but it’s supposed to be obsolete and I haven’t found any good simple demo.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T04:50:36+00:00Added an answer on May 30, 2026 at 4:50 am

    Everything that you need comes out of difflib — for example:

    >>> import difflib
    >>> d = difflib.Differ()
    >>> l = list(d.compare("hello", "heXXo"))
    >>> l
    ['  h', '  e', '- l', '- l', '+ X', '+ X', '  o']
    

    Each element in that list is a character from your two input strings, prefixed with one of

    • " " (2 spaces), character present at that position in both strings
    • "- " (dash space), character present at that position in the first string
    • "+ " (plus space), character present at that position in the second string.

    Iterate through that list and you can build exactly the output you’re looking to create.

    There’s no mention of difflib being in any way obsolete or deprecated in the docs.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to highlight a section of a street between two intersections. I have
I need to highlight changes(diff) between 2 database text fields in a asp.net application.
Can anyone recommend some good resources that highlight the differences between Oracle and the
I need to highlight, case insensitively, given keywords in a JavaScript string. For example:
I basically need to highlight a particular word in a block of text. For
I'm working on an asp.net application which uses Lucene.net I need to highlight the
I have a regular texbox control. I need to highlight some words with a
I need to highlight a particular column header in a JTable.Is it possible to
Seems Janus doesn't think a developer might have a need to highlight the value
I have a paragraph of text in which I need to highlight some key

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.