w3resource

Python: Get the current username

Python Basic: Exercise-54 with Solution

Write a Python program to get the current username.

Sample Solution:-

Python Code:

import getpass
print(getpass.getuser())

Sample Output:

trinket

Flowchart:

Flowchart: Get the current username.

Python Code Editor:

 

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a python program to access environment variables.
Next: Write a Python to find local IP addresses using Python's stdlib.

What is the difficulty level of this exercise?

Test your Python skills with w3resource's quiz



Python: Tips of the Day

Try-catch-else construct:

try:
  foo() 
except Exception: 
  print("Exception occured")
else:
  print("Exception didnt occur")
finally:
  print("Always gets here")