Aug 26, 2021

Python 3: run a script, string and number formatting

Python Language techniques

 

To run a python script from the terminal

1) create python script “cow.py”
2) Execute with
    python3 cow.py

Reference: https://howchoo.com/python/run-python-terminal

Python 3 string and number formatting: f-strings

A new, more compact and clear formatted print syntax appears in Python 3. 
  • Use the operator “f” in a print()
  • enclose variables in braces
  • Add number format modifiers after the variable, preceded by a “:”
    ex: formatting floats

        print(f'{x:5.2f}\t{y:6.2f}\t\t{z:8.2f}

f-strings: formatting large numbers with commas

Commas are often needed when formatting large numbers. The following shows the use of commas when numbers are aligned.
Example:

    number = 1000000  
    # print with comma separators  
    print(f'The number:, 1000000 {number:,.2f}')  
    # formatted with commas and right-aligned   
    print(f'The number, 1000000, in a width of 15 {number:>15,.2f}')
  
Also do... Example: The output of this code snippet is:
     TBD
?? missing


References:

https://cis.bentley.edu/sandbox/wp-content/uploads/Documentation-on-f-strings.pdf

No comments: