Formatting containers using format() in Python Last Updated : 06 Mar, 2023 Summarize Comments Improve Suggest changes Share Like Article Like Report Let us see how to format containers that were accessed through __getitem__ or getattr() using the format() method in Python. Accessing containers that support __getitem__a) For Dictionaries Python3 # creating a dictionary founder = {'Apple': 'Steve Jobs', 'Microsoft': 'Bill Gates'} # formatting print('{f[Microsoft]} {f[Apple]}'.format(f = founder)) Output : Bill Gates Steve Jobs f[Microsoft] is replaced by Bill Gates and f[Apple] is replaced by Steve Jobs. b) For lists Python3 # creating a list list_items = [1, 3, 5, 7, 9, 11] # formatting print('{l[3]} {l[5]}'.format(l = list_items)) Output : 7 11Accessing attributes on objects that support getattr()a) For Class Python3 # creating a class class Program(object): language = 'Python' # formatting print('{p.language}'.format(p = Program())) Output : Python p.language is replaced by Python as language is an attribute of Program Accessing the nested structure Python3 # creating a class class Program(object): language = 'Python' # creating a dictionary versions = [{'version': '1'}, {'version': '2'}, {'version': '3'}] # formatting print('{p.language}: {p.versions[2][version]}'.format(p = Program())) Output : Python: 3 Comment More infoAdvertise with us Next Article Formatting containers using format() in Python Y yuvraj_chandra Follow Improve Article Tags : Python Python-Built-in-functions Practice Tags : python Similar Reads Python code formatting using Black Writing well-formatted code is very important, small programs are easy to understand but as programs get complex they get harder and harder to understand. At some point, you canât even understand the code written by you. To avoid this, it is needed to write code in a readable format. Here Black come 3 min read Python String Formatting - How to format String? String formatting allows you to create dynamic strings by combining variables and values. In this article, we will discuss about 5 ways to format a string.You will learn different methods of string formatting with examples for better understanding. Let's look at them now!How to Format Strings in Pyt 9 min read What does %s mean in a Python format string? In Python, the %s format specifier is used to represent a placeholder for a string in a string formatting operation. It allows us to insert values dynamically into a string, making our code more flexible and readable. This placeholder is part of Python's older string formatting method, using the % o 3 min read How to Format date using strftime() in Python ? In this article, we will see how to format date using strftime() in Python. localtime() and gmtime() returns a tuple representing a time and this tuple is converted into a string as specified by the format argument using python time method strftime(). Syntax: time.strftime(format[, sec]) sec: This i 2 min read Python Modulo String Formatting In Python, a string of required formatting can be achieved by different methods. Some of them are; 1) Using % 2) Using {} 3) Using Template Strings In this article the formatting using % is discussed. The formatting using % is similar to that of 'printf' in C programming language. %d - integer %f - 2 min read Like