Jul-04-2019, 01:26 AM
Don't use this old method anymore.
f-string
is what you should use,was new in Python 3.6(2-year ago).subject = "Engineering" language = "Python" print(f"I am studying {subject} and using {language} as the programming language")
Output:I am studying Engineering and using Python as the programming language
11 year ago in 2.6 foramt()
was new.subject = "Engineering" language = "Python" print("I am studying {} and using {} as the programming language".format(subject, language))
Output:I am studying Engineering and using Python as the programming language
Not only are f-string far more readable more concise,and less prone to error than other ways of formatting,but they are also faster!>>> name = 'f-string' >>> print(f"String formatting is called {name.upper():*^20}") String formatting is called ******F-STRING****** # f-strings can take any Python expressions inside the curly braces. >>> cost = 99.75999 >>> finance = 50000 >>> print(f'Toltal cost {cost + finance:.2f}') Toltal cost 50099.76 >>> for word in 'f-strings are cool'.split(): ... print(f'{word.upper():~^20}') ... ~~~~~F-STRINGS~~~~~~ ~~~~~~~~ARE~~~~~~~~~ ~~~~~~~~COOL~~~~~~~~