Basic usage of Python f-string

f-string is a string literal added from python3.6. The feature is to add f or F at the beginning outside the character string.

Insert value:

>>> name = 'GAO'
>>> age = 24
>>> f'Hello{name}is,{age}I'm talented.'
'Hello GAO, is 24 years old.'

Insert expression:

>>> years_later = 10
>>> f'{years_later}Years later{age + years_later}I'm talented.'
'Ten years later, I will be 34 years old.'

Call a function:

>>> def one_year_later(current_age):
...     return current_age + 1
...
>>> f'next year{one_year_later(age)}I'm talented.'
'I will be 25 next year.'

Call the method:

>>> f'{name}To lowercase{name.lower()}is.'
'Lowercase GAO is gao.'

Multiline:

>>> jikoshoukai = f'Hello{name}is,{age}I'm talented.' \
...               f'{years_later}Years later{age + years_later}I'm talented.' \
...               f'next year{one_year_later(age)}I'm talented.' \
...               f'{name}To lowercase{name.lower()}is.'
>>> jikoshoukai
'Hello GAO, is 24 years old. Ten years later, I will be 34 years old. I will be 25 next year. Lowercase GAO is gao.'

Zero padding:

>>> a = 123
>>> f'8 digits padded with zeros: {a:08}'
'Zero padding: 00000123'

Nest (zero padding):

>>> for i in range(4,8):
...     f'Zero padding{i}digit: {a:0{i}}'
...
'4 digits padded with zeros: 0123'
'Zero padded 5 digits: 00123'
'6 digits padded with zeros: 000123'
'7 digits padded with zeros: 0000123'

Number of decimal places, number of valid digits:

>>> a = 123.456789
>>> f'4 digits after the decimal point: {a:.4f}'
'Decimal places: 123.4568'
>>> f'4 effective digits: {a:.4g}'
'Effective digit: 123.5'

Recommended Posts

Basic usage of Python f-string
Basic usage of flask-classy
Basic usage of Jinja2
Usage of Python locals ()
Basic usage of SQLAlchemy
Basic knowledge of Python
[Introduction to Python] Basic usage of lambda expressions
[Python] Correct usage of map
Basic usage of PySimple GUI
Sample usage of Python pickle
[Python] Correct usage of join
[Introduction to Python] Basic usage of the library matplotlib
Basic grammar of Python3 system (dictionary)
[python] Correct usage of if statement
Basic study of OpenCV with Python
[2020/06 latest version] Basic usage of python dependency management tool poetry
RF Python Basic_01
Basic Python writing
Basics of Python ①
Basics of python ①
Basic usage of Btrfs on Arch Linux
Copy of python
Basic grammar of Python3 system (character string)
Basic grammar of Python3 series (list, tuple)
Non-logical operator usage of or in python
Python Basic Course (at the end of 15)
RF Python Basic_02
Basic grammar of Python3 system (included notation)
[Python] Basic pattern and usage of if statement (comparison operator and Boolean operator)
Introduction of Python
Basic operation list of Python3 list, tuple, dictionary, set
This is the only basic review of Python ~ 1 ~
This is the only basic review of Python ~ 2 ~
This is the only basic review of Python ~ 3 ~
[Python] Class type and usage of datetime module
Basic story of inheritance in Python (for beginners)
Basic operation of Python Pandas Series and Dataframe (1)
[Python] Operation of enumerate
List of python modules
Python basic course (12 functions)
Python basic grammar / algorithm
Python Basic Course (7 Dictionary)
Python basic course (2 Python installation)
Basic sorting in Python
Unification of Python environment
Copy of python preferences
Python basic course (9 iterations)
Basics of Python scraping basics
Basic operation of pandas
Summary of pyenv usage
[python] class basic methods
[python] behavior of argmax
Python Basic Course (11 exceptions)
the zen of Python
Python basic course (6 sets)
Basic operation of Pandas
python decorator usage notes
Python3 cheat sheet (basic)
Installation of Python 3.3 rc1
Python Basic Course (Introduction)
Python basic memorandum part 2