Python

String Formatting With String.Format Method in Python

AKA.DM 2024. 10. 16. 07:45
반응형

Another way to format strings is the string.format method.

This method indicates the places we want to change on a string with curly braces.

For example, there is a variable named Linkedin below, and two sections are left blank with curly braces.

We will place values in these sections with the string.format method. So how we do it? 

 

We write the variable we want to format. Then we call the format method by putting a period.

We write whatever we want to appear in the blank spaces in the method parenthesis.

For example, there are two print functions below. Formatting was done according to its values and the string expression we wanted to come as output.

 

Values in format brackets have index numbers.

We can put these index numbers in curly braces in the string so that the output is as we want.

For example, below, the values in the format brackets were mixed.

In order for the output to be as we want, we worte the index numbers of the vlaues in the parenthese in  the curly brackets in the order we want.

As a result, we  got the ouput in the order we wanted. 

 

In the stirng.format method, the variable name can also be enclosed in the format brakets. 

반응형