filmov
tv
4 Ways To Format Strings In Python (2 Min) | Using Format Method

Показать описание
In this tutorial, you'll learn how to format strings in Python.
1. Using the % operator
2. Using the format() method with positions
3. Using the format() method without positions
4. Using the format() method without formatting
—
—
Video Transcript:
—
—
Hi guys, this is Abhi from Gokcedb. In this video, you're going to learn Four Ways to Format Strings in Python. Let's start by looking at our variables.
I'm using database variables of the typed string and a cost variable of the type float. The first way to format a string is by using a percent operator. For message one, I'm using three formatters, percent d represents an integer, percent s represents a string and percent f represents a float.
Let's see what happens when we print message one. As you can see, all three placeholders were replaced by the corresponding values in the parentheses. Note 6.2 f represents a total length of 6 with two decimal places.
If I were to change 6 to 8 it's gonna add two spaces in front of 4999. Replacing 8 with 18 will add 12 extra spaces. The second way to format a string is by the format method with positions.
Here, you specify the placeholders within the curly braces. Zero represents the first position. One represents the second position and so on.
The formatter after the colon suggests that type d represents an integer s represents a string and f represents a float. Printing message 2 should result in a similar output as message 1. The third way to format a string is by using the format method without positions.
Here, you notice the curly braces. I have specified positions in front of the colons. Printing message 3 should result in the exact same output as message 2.
The fourth way to format a string is by using the format method without positions or formatters. Here, I didn't specify anything within the curly braces. Watch what happens when I print a message 4.
You'll notice that the float value was printed as is without any formatting. There you have it. Make sure you like, subscribe, and turn on the notification bell.
Until next time.
1. Using the % operator
2. Using the format() method with positions
3. Using the format() method without positions
4. Using the format() method without formatting
—
—
Video Transcript:
—
—
Hi guys, this is Abhi from Gokcedb. In this video, you're going to learn Four Ways to Format Strings in Python. Let's start by looking at our variables.
I'm using database variables of the typed string and a cost variable of the type float. The first way to format a string is by using a percent operator. For message one, I'm using three formatters, percent d represents an integer, percent s represents a string and percent f represents a float.
Let's see what happens when we print message one. As you can see, all three placeholders were replaced by the corresponding values in the parentheses. Note 6.2 f represents a total length of 6 with two decimal places.
If I were to change 6 to 8 it's gonna add two spaces in front of 4999. Replacing 8 with 18 will add 12 extra spaces. The second way to format a string is by the format method with positions.
Here, you specify the placeholders within the curly braces. Zero represents the first position. One represents the second position and so on.
The formatter after the colon suggests that type d represents an integer s represents a string and f represents a float. Printing message 2 should result in a similar output as message 1. The third way to format a string is by using the format method without positions.
Here, you notice the curly braces. I have specified positions in front of the colons. Printing message 3 should result in the exact same output as message 2.
The fourth way to format a string is by using the format method without positions or formatters. Here, I didn't specify anything within the curly braces. Watch what happens when I print a message 4.
You'll notice that the float value was printed as is without any formatting. There you have it. Make sure you like, subscribe, and turn on the notification bell.
Until next time.
Комментарии