How to do it:
1: Connect the USB drive or external hard drive to your Mac.
2: Open "Disk Utility" from Applications or by using "Spotlight" search.

3: Select "View", then "Show All Devices".

4: Select the USB device or external drive itself.
Important: The device may appear with several levels in the menu on the left. Select the top level, which represents the physical device itself. For example, it may be named “USB Flash Disk Media”.
Do not select “Container disk…” or the volume with the name you previously assigned to the drive.

5: Once the drive is selected, click "Erase" at the top of Disk Utility.

6: Select the following:
- Name: Enter the desired name for the drive.
- Format: APFS (Encrypted)
- Create a password.
Important: Enter a unique and strong password and confirm it. Do not use a password that is similar to passwords you use elsewhere. Keep the password in a safe place. You will need it to access the contents of the device. - Scheme: GUID Partition Map
7: Click "Erase".
Once the formatting process is complete, the device is encrypted.

The next time the device is connected to a Mac, you will need to enter the password to access its contents.
Important to know
APFS is primarily designed for Mac. If the USB drive or external hard drive will also be used on Windows computers, this solution may not be suitable.