[Python3] Format the character string using the variable name as the key.

Implemented function

from inspect import currentframe,getargvalues

def varfmt(target,*argv):  
    return target.format(**getargvalues(currentframe().f_back).locals)  

[* Addition *] @shiracamus commented that python3.6 or later can be written in the following way. Thank you!

def hoge_aa_print(hoge, aaa):
    print(f"{hoge} {aaa}")

hoge_aa_print(10, 20)

[reference] https://qiita.com/AnchorBlues/items/f7725ba87ce349cb0382 https://docs.python.org/ja/3/library/inspect.html

example

from inspect import currentframe,getargvalues

def varfmt(target,*argv):  
    return target.format(**getargvalues(currentframe().f_back).locals)  


def hoge_aa_print(hoge,aaa):
    print(varfmt("{hoge} {aaa}",hoge,aaa))

hoge_aa_print(10,20)

In this way, you can format the character string using the variable name as the key.

Supplement

If you have any mistakes, Masakari, complaints, dismay, etc., please comment.

Recommended Posts

[Python3] Format the character string using the variable name as the key.
Get the variable name of the variable as a character string.
[Python 2/3] Parse the format string
python: Use the variables defined in the string format as they are
[Python] Get the variable name with str
Effective Python was, as the name implies, Effective
Python string format
Python string format
[Introduction to Python] How to write a character string with the format function
[Python] I tried to get the type name as a string from the type function
python variable expansion, format
<Python> A quiz to batch convert file names separated by a specific character string as part of the file name
Name identification using python
How to get the variable name itself in python
Cut a part of the string using a Python slice
Python indentation and string format
Remove double-byte spaces before and after the character string (python)
Python f character (formatted string)
String format with Python% operator
Summary of character string format in Python3 Whether to live with the old model or the new model
[Introduction to Python] How to split a character string with the split function
Format when passing a long string as an argument of python
Get the formula in an excel file as a string in Python
[python] What is the sorted key?
Extract the targz file using python
Try using the Python Cmd module
Get the host name in Python
How to input a character string in Python and output it as it is or in the opposite direction.
Easily exchange data between Python, R and Julia using the Feather format
The eval () function that calculates a string as an expression in python
[Introduction to Python] Thorough explanation of the character string type used in Python!
Try using the Wunderlist API in Python
Try using the Kraken API in Python
Behind the flyer: Using Docker with Python
[50 counts] Key transmission using Python for Windows
Python UTC ⇔ JST, character string (UTC) ⇒ JST conversion memo
Python memo using perl --format is convenient.
Tweet using the Twitter API in Python
# 5 [python3] Extract characters from a character string
[Python] How to invert a character string
[Python beginner memo] Python character string, path operation
Working with OpenStack using the Python SDK
[Pandas] Expand the character string to DataFrame
Reboot the router using Python, Selenium, PhantomJS
Basic grammar of Python3 system (character string)
Escape curly braces in the format string
Set the process name of the Python program
[Python] Get the character code of the file
Python basic course (4 numeric type / character string type)
Download files in any format using Python
[PowerShell] Get the reading of the character string
Get the hierarchy name using the OpenMaya iterator
python setup.py test the code using multiprocess
Use something other than a <br> string for the <br> dict key in Python
The key to deploying Flask apps using Python Buildpack on Cloud Foundry (Diego)
Divides the character string by the specified number of characters. In Ruby and Python.
Dynamically add CSV header as attribute name of Model class using setattr of python
python> datetime> Get the datetime (eg 20151130) format string> print today.strftime ("% Y% m% d") / print "{:% Y% m% d}" .format ( today)
Python version (PHP to Python) that deletes the subsequent character string from the specified character string (extension)