create a template string in python

In Python, a template string can be created using the str.format() method or by using f-strings (formatted string literals). Here is an example of how to create a template string using f-strings:

main.py
name = "John"
age = 30
template = f"My name is {name} and I am {age} years old"
print(template)
96 chars
5 lines

Output:

main.py
My name is John and I am 30 years old
38 chars
2 lines

Alternatively, you can use the str.format() method to create a template string:

main.py
name = "John"
age = 30
template = "My name is {} and I am {} years old".format(name, age)
print(template)
106 chars
5 lines

Output:

main.py
My name is John and I am 30 years old
38 chars
2 lines

In both cases, the curly braces {} are used as placeholders for the variables name and age. You can add more placeholders to the template string as needed, and pass in more variables to format() or use more f-strings.

gistlibby LogSnag