Kelsey
-1
Q:

How to use .format in python

# Newer f-string format
name = "Foo"
age = 12
print(f"Hello, My name is {name} and I'm {age} years old.")
# output :
# Hello, my name is Foo and I'm 12 years old.
26
string_a = "Hello"
string_b = "Cena"

# Index based:
print("{0}, John {1}"
      .format(string_a, string_b))
# Object based:
print("{greeting}, John {last_name}"
      .format(greeting=string_a, last_name=string_b))
12
# Python3 program to demonstarte 
# 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))  
13
'{:10.5}'.format('xylophone')
0
#The {} are replaced by the vairables after .format
new_string = "{} is string 1 and {} is string 2".format("fish", "pigs")
0

New to Communities?

Join the community