How to get username in Python?

Print Current User Account Name in Python

Here is the simple way to get the user name by using python code.

You can get your username without making any changes in the below code.

import getpass
username = getpass.getuser()
print (username)

This command directly gets the user name and returns the name as string. getpass.getuser. 

The code is tested in Windows environment. Might work as well in other operating systems.