Find the added part in a string

Hi guys
I have two strings “Denis” and “Denis Dal Soler” and I need to extract the “added” string “Dal Soler”.
Someone can help me please? It allows me to print a string only when it was updated and with only the string added.
Thanks
Denis

>>> a = "Denis"
>>> b = "Denis Dal Soler"
>>> c = b.replace(a, "")
>>> print(c)
' Dal Soler'
2 Likes

I’d use c = b.replace(a, "").strip() to remove the space.

2 Likes

@abessman perfect!
The programmer ability is workaround…

>>> a = "Denis"
>>> b = "Denis Dal Soler"
>>> c = b.replace(a, "")
>>> print(c)
' Dal Soler'

Not so fast:

 >>> a = "Denis"
 >>> b = "Denis Dal Soler (also known as Denis), but not Dennis"
 >>> c = b.replace(a, "")
 >>> c
 ' Dal Soler (also known as ), but not Dennis'

What does the OP actualy want to do here?

If they are after the “extra”, maybe they only care about the prefix, if
it is "Denis"?

In Python 3.10 and later you can do this:

 >>> a = "Denis"
 >>> b = "Denis Dal Soler (also known as Denis), but not Dennis"
 >>> b.removeprefix(a)
 ' Dal Soler (also known as Denis), but not Dennis'

In Python 3.9 and earlier you need to examine the string with
b.startswith(a), and then access b[len(a):] if the startswith
test is true.

Cheers,
Cameron Simpson cs@cskk.id.au

2 Likes