f-string Python example

f-strings is a feature introduced in Python 3.6 that allows you to embed expressions inside string literals, using the f prefix and curly braces. This is often used to format strings in a more concise and easier-to-read way than using the % operator or the .format() method.

For example, you can use f-strings to print the value of a variable inside a string like this:

name = "John"
print(f"Hello, {name}!")

This will print the string "Hello, John!".

You can also use f-strings to format the output of an expression. For example:

x = 10
y = 20
print(f"{x} + {y} = {x + y}")

This will print the string "10 + 20 = 30".

You can also use f-strings to format the output of a function call or a method call. For example:

import math

print(f"The square root of 4 is {math.sqrt(4):.2f}")

This will print the string "The square root of 4 is 2.00".

Python f-string format float 2 decimal places

To format a float to 2 decimal places in an f-string in Python, you can use the :.2f format specifier. For example:

x = 3.14159265
print(f"{x:.2f}")

This will print the float x with 2 decimal places, like this:
3.14

You can also use the :.2f format specifier to format the output of an expression or a function call. For example:

import math

print(f"The square root of 4 is {math.sqrt(4):.2f}")

This will print the string "The square root of 4 is 2.00".

Python f-string format float as integer

To format a float as an integer in an f-string in Python, you can use the d format specifier. This will truncate the decimal part of the float and print the integer value. For example:

x = 3.14159265
print(f"{x:d}")

This will print the integer value of the float x, like this:
3

Python format float with thousand separator

To format a float with a thousand separator in Python, you can use the f"{:,}" format string. This will add a comma as a thousand separator to the formatted float. For example:

x = 1234567.89
print(f"{x:,.2f}")

This will print the float x with 2 decimal places and a thousand separator, like this:
1,234,567.89

You can also use the f"{:,}" format string to format the output of an expression or a function call. For example:

import math

print(f"The square root of 100000000 is {math.sqrt(100000000):,.2f}")

Python f-string padding

To pad a string in an f-string in Python, you can use the <, >, or ^ characters to specify the alignment, and the width field to specify the width of the padded string. For example:

x = "hello"
print(f"{x:<10}")  # left-align with width 10
print(f"{x:>10}")  # right-align with width 10
print(f"{x:^10}")  # center with width 10

This will pad the string x with spaces to the specified width and alignment, like this:

hello    
     hello
  hello   

You can also use the f-strings feature to pad the output of an expression or a function call. For example:

import math

print(f"{math.pi:<10.2f}")  # left-align with width 10 and 2 decimal places
print(f"{math.pi:>10.2f}")  # right-align with width 10 and 2 decimal places
print(f"{math.pi:^10.2f}")  # center with width 10 and 2 decimal places

This will pad the float value of math.pi with spaces to the specified width and alignment, like this:

3.14     
      3.14
  3.14    

python f-string padding 0

You can use the f-strings feature to pad strings with other characters, such as zeros or asterisks. For example:

x = "hello"
print(f"{x:*<10}")  # left-align with width 10 and padding with asterisks
print(f"{x:0>10}")  # right-align with width 10 and padding with zeros
print(f"{x:^10}")  # center with width 10 and padding with spaces

This will pad the string x with the specified characters to the specified width and alignment, like this:

hello*****
0000000hello
  hello    

Leave a Comment