In this example, we are going to understand how to declare Python string variable f-string with examples. We need to use the f-string literal as the latest way to format a string in python. So first we will go through the f-string and its different ways to change the variable value at runtime.
Ways before f-string to format String
- % formatting
- str.format() method
Because of some limitations of these methods “f–string literal“ was introduced in python version 3.6.
1. How to use % formatting
In this example, we have shown how to use % formatting and Its limitation. It does not display dictionary and tuple correctly.
Python Program Example
name = "Max"
greet ="Good Morning!"
print("Hi, %s. Welcome, %s."%(name,greet))
Output
Hi, Max. Welcome, Good Morning!
2. How to use str.format()
In this example, we have shown how to use str.format(). It was added in python version 2.6.
Python Program Example
name = "Max"
greet ="Good Morning!"
print("Hi, {}. Welcome, {}.".format(name, greet))
Output
Hi, Max. Welcome, Good Morning!
3. What is f-string in Python
Python version 3.6 introduced the formatted string literal also known as “f-string”.The “f-string” is used for formatting the string and is easy to use and readable. To use it we have to start the string with “F” or “F” before quotation marks and write a python expression or variable that contains literal values between { } that are evaluated at runtime. The f-string controls the final output of the formatted string.
1. F-string to change string variable with self-correcting code
In this example, we have two string variables name and greet, We are formatting the string by using f-string literal at the start before the quotation mark. We have passed variables in between { } theses variable values are evaluated at runtime and how we can your final format string.
Python Program Example
name = "Max"
greet ="Good Morning!"
print( f"Hi, {name}. Welcome, {greet}.")
Output
Hi, Max. Welcome, Good Morning!.
2. F-string to change variable with self-correcting code to evalulate Expression
In this example, instead of variables, we have evaluated an expression by passing the expression between {}. All the given expressions will evaluate at runtime.
Python Program Example
print( f"{10* 2}")
print( f"{10+2}")
print( f"{10-6}")
print( f"{10%2}")
print( f"{10+5+6-8}")
Output
20
12
4
0
13
3. F-string in Class to change string variable with self-correcting code
In this example, we have defined a class, People where we have passed three arguments(full_name, country, age) in its constructor. The class constructor is called itself we create an instance of the class.
- Here we have created the instance/object of class newppl with all given parameters in the class constructor,
- so all the code is written in the class constructor we called.
- The f-string literal is used to format the string variable in the class constructor.
- The value of these variable will evaluate at runtime we will get our formatted string
Python Program Example
class People:
def __init__(self, fUll_name, country, age):
self.fUll_name = fUll_name
self.country = country
self.age = age
def __str__(self):
return f"You are welcome {self.fUll_name},Country is :{self.country}, age is : {self.age}."
newppl = People("Max", "United State", "55")
print(f"{newppl}")
Output
You are welcome Max,Country is :United State, age is 55.
4. f-string in function to change a string variable with self-correcting code
In this example, we have defined two methods Get_greet(inputStr) and Getname(name) calling this method by using f-string literal. To call we have to pass the method name between { } brackets and pass an argument to the method
The output return by these methods is evaluated at runtime.
Python Program Example
def Get_greet(inputStr):
return inputStr
def Getname(name):
return name
myname = "Simon Max"
greet = "Good Morning!"
print(f"{Get_greet(greet)} {Getname(myname)}")
Output
Good Morning! Simon Max
Summary
In this post, we have how to change string variable with self-correcting code python with example by using different method % formatting, str.format() method and “f-string” literal in Python.