Personal tips about Python (strings, around logs)

Around the string

format,% notation around

Easy to use format

'{0}, {1}, {2}...'.format(Variable 1,Variable 2,Variable 3 ....)  #Specified by index (subscript)

f string

The f string (f-strings) puts f or F before the string literal (f'xxx', F'xxx'). Variables can be specified as they are in the replacement field in the string.

print(f'{a} and {b}')
# 123 and abc

%notation

print('from %s to %s'(year1, year2))

Around input / output

Stop once and branch

In the case of execution from the command line, execution stops here and you are prompted to enter a number. If a non-numeric value is entered, the program will end there.

try:
    kai = int(input('How many times?Enter in half-width numbers:'))
except ValueError:
    print('Not a valid number.')
    quit()

if kai == 1:
    hoge
elif kai == 2:
    fuga

You can also put an argument after the file name when executing from the command line.

python hoge.py hoge1 hoge2

When you execute

import sys 
args = sys.argv
print(args)
#[hoge.py, hoge1, hoge2]

If you change the file name, the output directory and file name will be changed automatically.

#First get the file name
fname = os.path.basename(__file__)

#Cut out the relevant part from the file name
yyyy = fname[0:4]
mm = fname[4:6]
dd = fname[6:8]

Recommended Posts

Personal tips about Python (strings, around logs)
python tips
python tips
Python Tips
Python tips
Python Conda Tips
About python slices
About python comprehension
About Python tqdm.
Django's personal tips
About python, class
Python debugging tips
Personal tips when doing various things with Python 3
Python click tips
Unexpectedly (?) Python tips
About python inheritance
Tensorflow personal tips
About python, range ()
About python decorators
Python personal Q.A
python personal notes
About python reference
About Python decorators
[Python] About multi-process
About Python for loops
Summary about Python scraping
About function arguments (python)
Python and numpy tips
missingintegers python personal notes
About Python, for ~ (range)
About Python3 character code
Python Tips (my memo)
[Personal notes] Python, Django
[Python] Memo about errors
About Python development environment
Python: About function arguments
Python, about exception handling
Python PyTorch install tips
About Python Pyramid traversal
Memorize Python commentary 2 --Strings
Notes around Python3 assignments
About Python3 ... (Ellipsis object)
Compare strings in Python
Reverse strings in Python
[Python] Chapter 01-01 About Python (First Python)
[Python] About standard input
About __all__ in python
[Blender x Python] Blender Python tips (11/100)
Display character strings without line breaks in python (personal memo)