Get the App
SLTechnology News&Howtos  ›  Development  › 

What is the use of f-Strings in Python

Shulou Source: shulou.com Published: 2022-06-03 03:32:49 09月21日 Update

This article mainly introduces the use of f-Strings in Python, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let the editor take you to understand it.

1. Variable name str_value = "hello,python coders" print (f "{str_value =}") # str_value = 'hello Python coders'2, directly change the output num_value = 123print (f "{num_value% 2 =}") # num_value% 2 = 13, direct format date import datetime today = datetime.date.today () print (f "{today:% Y%m%d}") # 20211019 print (f "{today =:% Y%m%d}") # today = 202110194, The 2-8-16 binary output is really too simple > a = 42 > f "{avision b}" # 2 base '101010' > > f "{abureo}" # 8 base'52'> > f "{avision x}" # 16 base Lowercase letter'2a'> > f "{av X}" # hexadecimal Uppercase letters'2A'> > f "{ascii}" # ascii code'*'5, formatted floating point number > > num_value = 123.456 > f' {num_value =: .2f}'# keep 2 decimal places' num_value = 123.46'> nested_format = ".2f" # can be used as a variable > print (f'{num_value: {nested_format}') 123.466, String alignment > x = 'test' > f' {x: > 10}' # right alignment Fill in the blanks on the left 'test' > f' {XRV *

Tags: Base format article variable character string output at the same time letter highest elegant value interest uppercase uppercase lowercase decimal efficiency way date Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Linux Microsoft macOS vpn MySQL