0

Let's say I have a string like this:

s = "blah blah start=0 length=10"

and i have two variables named start = 0 and length = 100. Now, I want to form a new string (or replace the existing string) with:

s = "blah blah start="+str(start)+" length="+str(length)

but I want to do it dynamically, e.g. using the replace() method on string s or something similar and not by typing the above code by hand. That is, I want to form a new string with strings and variable names using python code. The string s changes quite often, as it is a url.

Is this possible?

1 Answer 1

0

I'd do it using a simple function

def change_blah_str(nstart, nlength):
    return f'blah blah start= {nstart} length= {nlength}'

I hope you like it

Sign up to request clarification or add additional context in comments.

2 Comments

The string s changes every so often. That' s why I need something like replacing the fixed values with the variable names using a method. I don't want to type again the new string in the method you propose. Thank you anyway.
You don't have to type it again, you just have to do: change_blah_str(10, 20) change_blah_str(15, 25) change_blah_str(40, 2) change_blah_str(53, 4)

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.