Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

string format()

# ------------------- string format, f-string ----------------------------

# {} is placeholder
num1 = 5
num2 = 3                
print(f'{num1} times {num2} is {num1 / num2:.2f}')  #2f means print to 2 decimal precision
#5 times 3 is 1.67

#explicit call format() method
number1 = 'One'
number2 = 'Two'
number3 = 'Three'

# default(implicit) order
default_order = "{}, {} and {}".format(number1,number2,number3)
print(default_order)
# One, Two and Three

# order using positional argument
positional_order = "{1}, {0} and {2}".format(number1,number2,number3)
print(positional_order)
# Two, One and Three

# order using keyword argument
keyword_order = "{i}, {j} and {k}".format(j=number1,k=number2,i=number3)
print(keyword_order)
# Three, One and Two
Comment

.format()

# Python3 program to demonstrate
# the str.format() method
 
# using format option in a simple string
print("{}, A computer science portal for geeks."
      .format("GeeksforGeeks"))
 
# using format option for a
# value stored in a variable
str = "This article is written in {}"
print(str.format("Python"))
 
# formatting a string using a numeric constant
print("Hello, I am {} years old !".format(18))
Comment

.format()

# Python program using multiple place
# holders to demonstrate str.format() method
 
# Multiple placeholders in format() function
my_string = "{}, is a {} science portal for {}"
print(my_string.format("GeeksforGeeks", "computer", "geeks"))
 
# different datatypes can be used in formatting
print("Hi ! My name is {} and I am {} years old"
      .format("User", 19))
 
# The values passed as parameters
# are replaced in order of their entry
print("This is {} {} {} {}"
      .format("one", "two", "three", "four"))

OUTPUT:
GeeksforGeeks, is a computer science portal for geeks
Hi! My name is User and I am 19 years old
This is one two three four
Comment

.format()

OUTPUT:
GeeksforGeeks, A computer science portal for geeks.
This article is written in Python
Hello, I am  18 years old!
Comment

.format()

# Python3 program to demonstrate
# the str.format() method
 
# using format option in a simple string
print("{}, A computer science portal for geeks."
      .format("GeeksforGeeks"))
 
# using format option for a
# value stored in a variable
str = "This article is written in {}"
print(str.format("Python"))
 
# formatting a string using a numeric constant
print("Hello, I am {} years old !".format(18))

OUTPUT:

GeeksforGeeks, A computer science portal for geeks.
This article is written in Python
Hello, I am  18 years old!
Comment

.format()

# Python program using multiple place
# holders to demonstrate str.format() method
 
# Multiple placeholders in format() function
my_string = "{}, is a {} science portal for {}"
print(my_string.format("GeeksforGeeks", "computer", "geeks"))
 
# different datatypes can be used in formatting
print("Hi ! My name is {} and I am {} years old"
      .format("User", 19))
 
# The values passed as parameters
# are replaced in order of their entry
print("This is {} {} {} {}"
      .format("one", "two", "three", "four"))

OUTPUT:
GeeksforGeeks, is a computer science portal for geeks
Hi! My name is User and I am 19 years old
This is one two three four
Comment

PREVIOUS NEXT
Code Example
Python :: # check built-in function using dir() 
Python :: #check if the element exists in the list.#check if the element exists in the list. 
Python :: # generators 
Python :: fill variable based on values of other variables python 
Python :: quadkey calculator 
Python :: This code is supposed to display "2 "2 + 2 = 4"" on the screen, but there is an error. Find the error in the code and fix it, so that the output is correct. 
Python :: cornell hotel sustainability benchmarking index 
Python :: pandas dataframe how to store 
Python :: colorutils python 
Python :: two lists with identical entries get order 
Python :: changing speak rate pyttsx 
Python :: auto reload exml odoo 13 
Python :: Find Factors of a Number using While Loop with validation 
Python :: PHP echo multi lines Using Heredoc variable 
Python :: how to use js in python 
Python :: joining datasets by id python 
Python :: unique lits on python 
Python :: empty python 
Python :: drop columns delta table 
Python :: how to write together string type value and int type value in print function in python 
Python :: Python NumPy atleast_1d Function Example when inputs are in high dimension 
Python :: conmbination in python 
Python :: Python NumPy ascontiguousarray Function Example List to an array 
Python :: objects list 
Python :: python how to loop through array 
Python :: python cos not the same as calculator 
Python :: import date formater 
Python :: Printing a long code line to span over multiple lines 
Python :: how to calculate iqr in pandas 
Python :: raspberry pi set python 3 as default 
ADD CONTENT
Topic
Content
Source link
Name
8+8 =