Skip to main content

Standard Normal Distribution with examples using Python

Easy Guide to using f-strings in Python with examples

Easy Guide to using f-strings in Python with examples

f-strings were introduced in Python 3.6. String formatting has been made simpler with f-strings. f-strings are of the form f" " or f' '. Let's look at some examples to understand this better.

How to print a variable using f-strings?

  
name = "James Bond"
drink = "martini"
serving_type = "shaken and not stirred"
f"My name is {name} and I like my {drink} {serving_type}"
--->My name is James Bond and I like my martini shaken and not stirred

f-strings use a { } to evaluate any expression and format it as a string. The above example evaluates each of the variables within curly braces and formats it as string.

How to use mathematical expressions with f-strings

  
f"2x2 = {2*2}"
--->2x2 = 4
f"Square of 8 is {8**2}
---> Square of 8 is 64

Practically anything can be put inside the { } as long as the expresson evaluates to something concrete. 2*2 and 8**2 are evaluated and presented in string format. Is this all which can be done? What else can be done?

How to make a function call within f-strings?


def square_of_no(x):
  return x**2
f"Square of 8 is {square_of_no(8)}"
--->Square of 8 is 64

How to make use of lambda function within f-strings?


f"Square of 8 is {(lambda x:x**2)(8)}"
--->Square of 8 is 64
f"Sum of a^2 and b^2 is { (lambda x,y: x**2+y**2)(4,6) }"
---> Sum of a^2 and b^2 is 52

How to use dictionaries within f-strings?


dict_age = {'Rohit':38, 'Harry':43, 'Samarth': 7, Jessy': 56}
f"Age of Rohit is {dict_age.get('Rohit')} years and Age of Samarth is {dict_age.get('Samarth')} years"
--->Age of Rohit is 38 years and Age of Samarth is 7 years

We will slightly modify the above code now. Observe that we are now referencing the keys of dictionary with double quotes " " instead of single quotes ' '.


dict_age = {'Rohit':38, 'Harry':43, 'Samarth': 7, 'Jessy': 56}
f"Age of Rohit is {dict_age.get("Rohit")} years and Age of Samarth is {dict_age.get("Samarth")} years"
--->f"Age of Rohit is {dict_age.get("Rohit")} years and Age of Samarth is   {dict_age.get("Samarth")} years"
SyntaxError: invalid syntax

We used double quotes to begin the f-string syntax. Hence we cannot reference keys of the dictionary using double quotes. It can be referenced using single quotes only. On the contrary, if we begin the f-string using single quotes, we can only use double quotes to reference the keys of the dictionary. Let's see this in action.


f'Age of Rohit is {dict_age.get("Rohit")} years and Age of Samarth is {dict_age.get("Samarth")}'
---> Age of Rohit is 38 years and Age of Samarth is 7
f"Age of Rohit is {dict_age.get('Rohit')} years and Age of Samarth is {dict_age.get('Samarth')} years"
--->Age of Rohit is 38 years and Age of Samarth is 7 years

How to use Multiline f-strings in Python?

  
day1 = "Sunday"
day2 = "Monday"
info = f"Today is {day1}."
   f"Tomorrow is {day2}."
   f"I hope {day1} never ends."
print(info)

Using an Escape character \ is equally valid after each line.

  
day1 = "Sunday"
day2 = "Monday"
info = f"Today is {day1}."\
      f"Tomorrow is {day2}."\
      f"I hope {day1} never ends.  "
print(info)
---> Today is Sunday.Tomorrow is Monday.I hope Sunday never ends.  

It is important to put f" " on each line. Otherwise results may not be as expected. Let's see what happens if we do that.

  
day1 = "Sunday"
day2 = "Monday"
info = f"Today is {day1}."\
   "Tomorrow is {day2}."\
   "I hope {day1} never ends."
print(info)
--->Today is Sunday.Tomorrow is {day2}.I hope {day1} never ends.

If we want each sentence on a new line, we use \n character

    
day1 = "Sunday"
day2 = "Monday"
info = f"Today is {day1}.\n"\
      f"Tomorrow is {day2}.\n"\
      f"I hope {day1} never ends.\n "
print(info)
--->Today is Sunday.
    Tomorrow is Monday.
    I hope Sunday never ends.

If you want to know about formatting numerical and float values using f-strings, you can have a look at this post.

Comments

Popular posts from this blog

How to adopt Embeddings for Categorical features in Tabular Data using PyTorch's nn.Embedding( )-- Part 2

In the previous post , we set up the context to utilize embeddings for categorical features. In this post, we will figure out how to create these embeddings and combine them with other continuous features to build a neural network model. Dataset Download We will utilize the UCI machine learning repo which has a dataset on credit card default for customers in Taiwan. This dataset is also available in Kaggle . Metadata about this dataset is available on the respective websites. To follow this post, it is recommended to download the dataset from Kaggle. Most of the features are self explanatory. Embedding Creation A few definitions first. Levels in a categorical feature represent unique values available for that categorical feature. For e.g. MARRIAGE has levels 0,1,2,3. Each level of a categorical feature is represented by a vector of numbers. So, if you stack up all the levels together and all the vectors together, you can imagine levels to be a colum

How to adopt Embeddings for Categorical features in Tabular Data using PyTorch's nn.Embedding( )-- Part 1

How to adopt Embeddings for Categorical features in Tabular Data using PyTorch's nn.Embedding( )-- Part 1 In this post, we will talk about using embeddings for categorical features using PyTorch. This post will be broken down into following parts. Dataset Download Data Understanding Data Preprocessing Embedding Creation Define Dataset and Dataloaders in PyTorch Neural Network definition in PyTorch The Training Loop Model Validation The idea about using Embeddings from Categorical Features was first mooted during a Kaggle contest and a paper was also published on this. In the context of NLP and word embeddings, we represent each word in an n dimesnional vector space. In a similar way, we can represent any categorical feature in an n dimesnional vector space as well. 1. Dataset Download We will utilize the UCI machine learning repo which has a dataset on credit card default for customers in Taiwan. This dataset is also av

Standard Normal Distribution with examples using Python

Standard Normal Distribution with examples In our previous post, we talked about Normal Distribution and its properties . In this post, we extend those ideas and discuss about Standard Normal Distribution in detail. What is a Standard Normal Distribution? A Normal Distribution with mean 0 and standard deviation 1 is called a Standard Normal Distribution . Mathematicallty, it is given as below. Fig 1:Standard Normal Probability Distribution Function For comparison, have a look at the Normal Probability Distribution Function. If you substitute mean as 0 ,standard deviation as 1, you derive the standard normal probability distribution function Fig 2: Normal Probability Distribution Function Need for a standard normal probability distribution function We need to extract probability information about events that we are interested in. For this, first we need to convert any normal random variable