Formatting containers using format() in Python Last Updated : 06 Mar, 2023 Comments Improve Suggest changes 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 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 Convert integer to string in Python In this article, weâll explore different methods for converting an integer to a string in Python. The most straightforward approach is using the str() function.Using str() Functionstr() function is the simplest and most commonly used method to convert an integer to a string.Pythonn = 42 s = str(n) p 2 min read Format a Number Width in Python Formatting numbers to a fixed width is a common requirement in various programming tasks, especially when dealing with data presentation or storage. By understanding these techniques, developers can ensure consistent and visually appealing formatting of numerical data in their Python. Format a Numbe 3 min read Python String Interpolation String Interpolation is the process of substituting values of variables into placeholders in a string. Let's consider an example to understand it better, suppose you want to change the value of the string every time you print the string like you want to print "hello <name> welcome to geeks for 4 min read Python String center() Method center() method in Python is a simple and efficient way to center-align strings within a given width. By default, it uses spaces to fill the extra space but can also be customized to use any character we want. Example:Pythons1 = "hello" s2 = s1.center(20) print(s2)Output hello Explanation: Here, the 2 min read Python String Concatenation String concatenation in Python allows us to combine two or more strings into one. In this article, we will explore various methods for achieving this. The most simple way to concatenate strings in Python is by using the + operator.Using + OperatorUsing + operator allows us to concatenation or join s 3 min read Like