String formatting in Python
Strings are simple! We can just enclose whatever we want into quotes and it becomes a string. But things get complex when we want to create complex strings that may contain many escape characters and variables. In such cases, Python's String formatting functions come to our help. Let us consider an example of a string concatenated with variables name = 'Aaron' city = 'Chennai' s = 'Hey ' + name + ', ' + city + ' Welcomes you' print(s) Output: Hey Aaron, Chennai Welcomes you Here in Line 3, we have joined various bits of strings and variables using the '+' operator to get the desired output. But this method is so complex and can lead to mistakes. Instead of hardwiring each part of the string, we can use string formatting methods to format our strings effectively the way we want. 1. The str.format() method: This method of formatting strings is more simple than the previous method. In this method, we define the place...