How to connect Php docker container with Mongo DB docker container
Goto your command terminal. Type: This will expose port: 27017 by default. You…
May 14, 2019
This post is dedicated for cases where we intend to append a variable value in a string, whether in logs, string message etc.
Just put a character f before your string, and put all variable in between curly braces.
Example
website_name = 'GyanBlog'
year = 2019
print (f'This website: {website_name} has written this post in year: {year}')
>>> This website: GyanBlog has written this post in year: 2019
This is fantastic way of using string formatting, and is quite easily readable too.
Lets look at above example
website_name = 'GyanBlog'
year = 2019
print ('This website: %s has written this post in year: %d' %(website_name, year))
# Note: %s for string, and %d for integers
Its kind of same as above. But, you use curly braces instead of percentage format.
website_name = 'GyanBlog'
year = 2019
print ('This website: {} has written this post in year: {}'.format(website_name, year))
You can use numbers in curly braces if you want to specify which variables comes when:
print ('This website: {0} has written this post in year: {1}. Thanks from {0}'.format(website_name, year))
>>> This website: GyanBlog has written this post in year: 2019. Thanks from GyanBlog
print ('This website: {1} has written this post in year: {0}'.format(website_name, year))
>>> This website: 2019 has written this post in year: GyanBlog
#Note: the position number in curly braces
But, you can not mix them!
print ('This website: {1} has written this post in year: {}'.format(website_name, year))
# error: ValueError: cannot switch from manual field specification to automatic field numbering
You can concatenate strings simply by + operator.
s = 'hi' + ' ' + 'GyanBlog'
print(s)
>>> 'hi GyanBlog'
This is a simple way, but doesn’t look good.
Goto your command terminal. Type: This will expose port: 27017 by default. You…
Introduction This post is about hosting ElasticSearch cluster on dockerised…
I was testing a bug where a field was limited to 255 characters only. I needed…
Introduction Twig is a powerful template engine for php. Drupal uses it heavily…
Introduction In this post, we will see how to theme form and its fields…
Introduction In this guide, We will get basic understanding of various options…
In this post, we will see some of the frequently used concepts/vocabulary in…
System design interview is pretty common these days, specially if you are having…
Introduction You are given an array of integers with size N, and a number K…
Graph Topological Sorting This is a well known problem in graph world…
Problem Statement Given a Binary tree, print out nodes in level order traversal…
Problem Statement Given an array nums of n integers and an integer target, are…