Approach 1: Using curly braces
str1 = 'Hi, I am {} and {} years old. I am from {}'.format('Krishna', 31, 'India')
Approach 2: Using positional arguments
str2 = 'Hi, I am {1} and {0} years old. I am from {2}'.format( 31, 'Krishna', 'India')
Approach 3: Using custom symbols
str3 = 'Hi, I am {name} and {age} years old. I am from {country}'.format( age=31, name='Krishna', country='India')
Find the below working application.
format_strings.py
str1 = 'Hi, I am {} and {} years old. I am from {}'.format('Krishna', 31, 'India')
str2 = 'Hi, I am {1} and {0} years old. I am from {2}'.format( 31, 'Krishna', 'India')
str3 = 'Hi, I am {name} and {age} years old. I am from {country}'.format( age=31, name='Krishna', country='India')
print(str1)
print(str2)
print(str3)
Hi, I am Krishna and 31 years old. I am from India Hi, I am Krishna and 31 years old. I am from India Hi, I am Krishna and 31 years old. I am from India
No comments:
Post a Comment