Skip to content
Advertisement

Python – can you “refresh” a variable to re-initialize with new sub-variables?

Say i want to declare a string for my entire class to use, but that string has a part that might change later. Is it possible to only have to declare the string once, and “refreshing” it later?

Example:

JavaScript

Can i adjust this code so that in the end, it prints out This is a long string using another newer, better substring in it., without declaring the long string again later on?

My only guess would be something like this:

JavaScript

But i want to ask if there possibly is a built-in function in Python to do exactly that?

Advertisement

Answer

Don’t use f-string, declare the template string with place holder {} and use format() where necessary

JavaScript

You can insert as many substrings as you want that way

JavaScript

You can also use variables

JavaScript
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement