Cara menggunakan python logging string interpolation

String interpolation is a process substituting values of variables into placeholders in a string. For instance, if you have a template for saying hello to a person like "Hello {Name of person}, nice to meet you!", you would like to replace the placeholder for name of person with an actual name. This process is called string interpolation.

f-strings

Python 3.6 added new string interpolation method called literal string interpolation and introduced a new literal prefix

Hello World! This is Python
6. This new way of formatting strings is powerful and easy to use. It provides access to embedded Python expressions inside string constants.

Example 1:

name = 'World'
program = 'Python'
print(f'Hello {name}! This is {program}')

When we run the above program, the output will be

Hello World! This is Python

In above example literal prefix

Hello World! This is Python
6 tells Python to restore the value of two string variable name and program inside braces
Hello World! This is Python
8. So, that when we
Hello World! This is Python
9 we get the above output.

This new string interpolation is powerful as we can embed arbitrary Python expressions we can even do inline arithmetic with it.

Example 2:

a = 12
b = 3
print(f'12 multiply 3 is {a * b}.')

When we run the above program, the output will be

12 multiply 3 is 36.

In the above program we did inline arithmetic which is only possible with this method.


%-formatting

Strings in Python have a unique built-in operation that can be accessed with the

a = 12
b = 3
print(f'12 multiply 3 is {a * b}.')
0 operator. Using
a = 12
b = 3
print(f'12 multiply 3 is {a * b}.')
0 we can do simple string interpolation very easily.

Example 3:

print("%s %s" %('Hello','World',))

When we run the above program, the output will be

Hello World

In above example we used two

a = 12
b = 3
print(f'12 multiply 3 is {a * b}.')
2 string format specifier and two strings
a = 12
b = 3
print(f'12 multiply 3 is {a * b}.')
3 and
a = 12
b = 3
print(f'12 multiply 3 is {a * b}.')
4 in parenthesis
a = 12
b = 3
print(f'12 multiply 3 is {a * b}.')
5. We got
a = 12
b = 3
print(f'12 multiply 3 is {a * b}.')
6 as output.
a = 12
b = 3
print(f'12 multiply 3 is {a * b}.')
2 string format specifier tell Python where to substitute the value.

String formatting syntax changes slightly, if we want to make multiple substitutions in a single string, and as the

a = 12
b = 3
print(f'12 multiply 3 is {a * b}.')
0 operator only takes one argument, we need to wrap the right-hand side in a tuple as shown in the example below.

Example 4:

name = 'world'
program ='python'
print('Hello %s! This is %s.'%(name,program))

When we run the above program, the output will be

Hello world! This is python.

In above example we used two string variable name and program. We wrapped both variable in parenthesis

a = 12
b = 3
print(f'12 multiply 3 is {a * b}.')
5. 

It’s also possible to refer to variable substitutions by name in our format string, if we pass a mapping to the

a = 12
b = 3
print(f'12 multiply 3 is {a * b}.')
0 operator:

Example 5:

name = 'world' 
program ='python'
print(‘Hello %(name)s! This is %(program)s.’%(name,program))

When we run the above program, the output will be

Hello world! This is python.

This makes our format strings easier to maintain and easier to modify in the future. We don’t have to worry about the order of the values that we’re passing with the order of the values that are referenced in the format string.


Str.format()

In this string formatting we use

12 multiply 3 is 36.
1 function on a string object and braces
Hello World! This is Python
8, the string object in
12 multiply 3 is 36.
1 function is substituted in place of braces
Hello World! This is Python
8. We can use the
12 multiply 3 is 36.
1 function to do simple positional formatting, just like
a = 12
b = 3
print(f'12 multiply 3 is {a * b}.')
0 formatting.

Example 6:

Hello World! This is Python
0

When we run the above program, the output will be

Hello World! This is Python
1

In this example we used braces

Hello World! This is Python
8 and
12 multiply 3 is 36.
1 function to pass name object .We got the value of name in place of braces
Hello World! This is Python
8 in output.

We can refer to our variable substitutions by name and use them in any order we want. This is quite a powerful feature as it allows for re-arranging the order of display without changing the arguments passed to the format function.

Example 7:

Hello World! This is Python
2

When we run the above program, the output will be

Hello World! This is Python
3

In this example we specified the variable substitutions place using the name of variable and pass the variable in

12 multiply 3 is 36.
1.


Template Strings

Template Strings is simpler and less powerful mechanism of string interpolation. We need to import

print("%s %s" %('Hello','World',))
1 class from Python’s built-in
print("%s %s" %('Hello','World',))
2 module to use it.

Example 8:

Hello World! This is Python
4

When we run the above program, the output will be

Hello world! This is python.

In this example we import

print("%s %s" %('Hello','World',))
1 class from built-in
print("%s %s" %('Hello','World',))
2 module and made a template which we used to pass two variable.