xxxxxxxxxx
#The {} are replaced by the vairables after .format
new_string = "{} is string 1 and {} is string 2".format("fish", "pigs")
xxxxxxxxxx
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))
xxxxxxxxxx
Name = 'Tame Tamir'
Age = 14
Formatted_string = 'Hello, my name is {name}, I am {age} years old.'.format(name=Name,age=Age)
# after the formatting, the variable name inside the {} will be replaced by whatever you declare in the .format() part.
print(Formatted_string) # output = Hello, my name is Tame Tamir, I am 14 years old.
xxxxxxxxxx
# Basic usage
name = "John"
age = 30
print("My name is {} and I am {} years old".format(name, age))
# Specifying placeholders by index
print("My name is {0} and I am {1} years old. {0} is my first name".format(name, age))
# Specifying placeholders by name
print("My name is {name} and I am {age} years old".format(name=name, age=age))
# Formatting numbers
x = 123.456
print("The value of x is {:.2f}".format(x))
In the first example, we're using the format() function to insert the values of name and age into a string. The resulting string will contain the values of the variables in the correct places.
In the second example, we're using index-based placeholders to specify the order of the variables in the string.
In the third example, we're using named placeholders to make the code more readable.
In the fourth example, we're formatting a number (x) to two decimal places using the :.2f format specifier. The resulting string will contain the value of x with two decimal places.
xxxxxxxxxx
print('The {2} {1} {0}'.format('fox', 'brown', 'quick'))
result = 100/777
print('{newvar}'.format(newvar = result))
print('the result was {r:0.3f}'.format(r = result))
xxxxxxxxxx
# Python string format() method
# default(implicit) order
default_order = "{}, {} and {}".format('John','Bill','Sean')
print('\n--- Default Order ---')
print(default_order)
# order using positional argument
positional_order = "{1}, {0} and {2}".format('John','Bill','Sean')
print('\n--- Positional Order ---')
print(positional_order)
# order using keyword argument
keyword_order = "{s}, {b} and {j}".format(j='John',b='Bill',s='Sean')
print('\n--- Keyword Order ---')
print(keyword_order)
xxxxxxxxxx
# Python string арга .format() нь мөр дэх хоосон хаалт ({}) орлуулагчийг аргументуудаараа сольдог.
# Түлхүүр үгсийг орлуулагчид заасан бол аргын харгалзах нэртэй аргументуудаар солино.
msg1 = 'Fred scored {} out of {} points.'
msg1.format(3, 10)
# => 'Fred scored 3 out of 10 points.'
msg2 = 'Fred {verb} a {adjective} {noun}.'
msg2.format(adjective='fluffy', verb='tickled', noun='hamster')
# => 'Fred tickled a fluffy hamster.'
xxxxxxxxxx
# default arguments
print("Hello {}, your balance is {}.".format("Adam", 230.2346))
# positional arguments
print("Hello {0}, your balance is {1}.".format("Adam", 230.2346))
# keyword arguments
print("Hello {name}, your balance is {blc}.".format(name="Adam", blc=230.2346))
# mixed arguments
print("Hello {0}, your balance is {blc}.".format("Adam", blc=230.2346))