Categories
Python Answers

How to format a floating number to a fixed width in Python?

Sometimes, we want to format a floating number to a fixed width in Python.

In this article, we’ll look at how to format a floating number to a fixed width in Python.

How to format a floating number to a fixed width in Python?

To format a floating number to a fixed width in Python, we can use a special formatting code with the string’s format method.

For instance, we write:

numbers = [23.23, 0.1233, 1.0, 4.223, 9887.2]

for x in numbers:
    print("{:10.3f}".format(x))

to format all the numbers in numbers to strings with 3 decimal places.

The {:10.3f} formatting code rounds each number to 3 decimal places.

Therefore, we see:

   23.230
    0.123
    1.000
    4.223
9887.200

printed.

Conclusion

To format a floating number to a fixed width in Python, we can use a special formatting code with the string’s format method.

Categories
Python Answers

How to find the total memory used by a Python process?

Sometimes, we want to find the total memory used by a Python process.

In this article, we’ll look at how to find the total memory used by a Python process.

How to find the total memory used by a Python process?

To find the total memory used by a Python process, we can yue the os and psutil modules.

We install the psutil module by running:

pip install psutil

Then, we write:

import os, psutil

process = psutil.Process(os.getpid())
print(process.memory_info().rss)

We use the psutil.Process constructor with the process ID of our script which we get by using os.getpid.

Then we get the memory used with process.memory_info().rss.

Therefore, we should see the number in bytes of the amount of memory used by our script.

Conclusion

To find the total memory used by a Python process, we can yue the os and psutil modules.

Categories
Python Answers

How to convert an int to binary string with Python?

Sometimes, we want to convert an int to binary string with Python .

In this article, we’ll look at how to convert an int to binary string with Python.

How to convert an int to binary string with Python?

To convert an int to binary string with Python, we can use the string’s format method.

For instance, we write:

b = "{0:b}".format(37)
print(b)

We use the {0:b} formatting code to convert an int to a binary string.

Therefore, b is 100101, which is 37 in binary.

Conclusion

To convert an int to binary string with Python, we can use the string’s format method.

Categories
Python Answers

How to print string to a text file with Python?

Sometimes, we want to print string to a text file with Python.

In this article, we’ll look at how to print string to a text file with Python.

How to print string to a text file with Python?

To print string to a text file with Python, we can use the open and write methods.

For instance, we write:

with open("output.txt", "w") as text_file:
    text_file.write("foo")

We open the file we want to write to with a with statement and the open function.

We pass in the path of the text file and the permission as arguments of open.

In the block, we call text_file.write to write the string into the text file.

text_file is the file handle for the text file.

And the file will be closed automatically once the write operation is done since we used the with statement.

Conclusion

To print string to a text file with Python, we can use the open and write methods.

Categories
Python Answers

How to print the full Python NumPy array without truncation?

Sometimes, we want to print the full Python NumPy array without truncation.

In this article, we’ll look at how to print the full Python NumPy array without truncation.

How to print the full Python NumPy array without truncation?

To print the full Python NumPy array without truncation, we can use the numpy.setoptions method.

For instance, we write:

import sys
import numpy

numpy.set_printoptions(threshold=sys.maxsize)
l = numpy.arange(100)
print(l)

to set the print options to print everything with:

numpy.set_printoptions(threshold=sys.maxsize)

Then we define an array with evenly spaced list from 0 to 100 with `arange.

Therefore, l is:

[ 0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
 96 97 98 99]

Conclusion

To print the full Python NumPy array without truncation, we can use the numpy.setoptions method.