xxxxxxxxxx
# 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))
xxxxxxxxxx
# 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
xxxxxxxxxx
OUTPUT:
GeeksforGeeks, A computer science portal for geeks.
This article is written in Python
Hello, I am 18 years old!
xxxxxxxxxx
# 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!
xxxxxxxxxx
# 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