user2678648
user2678648

Reputation: 169

Difference between two string as substring in python

I have two filepath as strings

string1 = r"C:\Users\Admin"  
string2= "C:\Users\Admin\Context1\Context2\filepath.dat"

How can I get the difference as "Context1\Context2\filepath.dat"

As of now I am iterating string1 by characters and comparing with string2 when it fails I am returning string2[index:]

there should be something easier pythonic way to do this. Or point me to the documentation. Please see me as novice in python. Any help is much appreciated

Upvotes: 1

Views: 2079

Answers (2)

Daniele Bacarella
Daniele Bacarella

Reputation: 464

Also In my case string1 is always going to be ancestral root of string2

string2[len(string1)+1:]

Upvotes: 1

chakradhar kasturi
chakradhar kasturi

Reputation: 689

As simple as replacing the string1 with empty string in string2

string2.replace(string1,"")

Upvotes: 3

Related Questions