f-string Python

Содержание
Введение
Синтаксис
Пример
!r
Похожие статьи

Введение

f-string введены в Pytho 3.6 и описаны в PEP 498

Синтаксис

f ' <text> { <expression> <optional !s, !r, or !a> <optional : format specifier> } <text> ... '

Пример

value = 1 print(f'The value is {value}.') # Formatting value = 1234 print(f'input={value:#06x}') import datetime date = datetime.date(1991, 10, 12) print(f'{date} was on a {date:%A}') # Escaping with \ is not allowed print(f"{'quoted string'}")

python f-string.py

The value is 1. input=0x04d2 1991-10-12 was on a Saturday quoted string

!r

С помощью !r можно вывести repr()

# !r a = "AAA" print(f"{a}") print(f"{a!r}")

python f-string.py

AAA 'AAA'

Article author: Andrei Olegovich

Похожие статьи
Built-in Collections
Строки
Python
f-string
format string
encode()
Перенос слишком длинной строки на новую

Search this site

Subscribe to Telegram channel @aofeed to follow new articles and updates to old ones

Go to channel

@aofeed

Ask a question in the Telegram group

@aofeedchat

Contacts and cooperation:
I recommend our hosting beget.ru
Write to info@urn.su, info@aredel.com if you:
1. Want to write an article for our site or translate an article into your native language.
2. Want to place thematically relevant ads on the site.
3. Ads on my site pass maximum censorship. If you see an ad block unsuitable for school-age children, shocking or misleading - please contact us by e-mail
4. Found a mistake, inaccuracy, bug, etc. on the site. ... .......
5. Articles can be shared on social media by clicking a network icon: