0

I have a string as:

string1 = '../data/annotation/product_Aca_ma_MBIC11017.txt'

It is basically a path for the file that I will use later.

I want to add string2 = 'fake_' to string1 at a particular position, to make it look like:

'../data/annotation/fake_product_Aca_ma_MBIC11017.txt'

So far, I did:

string1 = string2+string1

It outputs as:

'fake_../data/annotation/product_Aca_ma_MBIC11017.txt'

What should I do to add string2 at a particular position of string1?

1 Answer 1

5

You are manipulating paths, so use os.path to split and rejoin:

dir, filename = os.path.split(string1)
string1 = os.path.join(dir, string2 + filename)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.