banovski

print counterpart

Feb 6th, 2022 (edited)
536
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.61 KB | None | 0 0
  1. #! /usr/bin/env python3
  2.  
  3. # Task: define a function that imitates "print". The function should
  4. # support such arguments as "sep", "end" and "file"
  5.  
  6. def prnt(*args, sep=' ', end='\n', file=None):
  7.     if "sys" not in dir():
  8.         import sys
  9.  
  10.     if file:
  11.         output_file = open(file, "w")
  12.     else:
  13.         output_file = sys.stdout
  14.  
  15.     for arg in args:
  16.         if arg == args[-1]:
  17.             output_file.write(arg)
  18.         else:
  19.             output_file.write(arg + sep)
  20.  
  21.     output_file.write(end)
  22.  
  23. prnt("foo", "bar", "baz", sep = '_', end = "…\n", file="./test.txt")
  24.  
  25. # foo_bar_baz…
  26. #
  27.  
Add Comment
Please, Sign In to add comment