Thursday, March 14, 2024

Argunents using in Print function

 Some Argunents using in Print function 

End inside print function : You can already create print statements like a boss, but there are a few things you can do to make them easier.Let's add a few secret second arguments to the print statement and see what happens.

By default, at the end of every print statement, the computer clicks 'enter'.

How a range of numbers shows each number on a new line...
for i in range(0, 100):
  print(i)

Here output as 0,1,2,3,....99 will be printed in a vertical manner .

Add a space
Let's tweak that code and see if we can get it to print with a space between each number instead of a new line. What do you notice?
for i in range(0, 100):
  print(i, end=" ")

Here output as 0,1,2,3,....99 will be printed in a horizontal manner with a space in between numbers.

Add a space and comma 
What if we want to add a comma and a space? Let's try it by adding , to our argument.
for i in range(0, 100):
  print(i, end=", ")

Here output as 0,1,2,3,....99

Add a new line, tab, or vertical tab
What happens if you add these different options in your second argument? Play around with these options below and see what they do:

#new line
for i in range(0, 100):
  print(i, end="\n")

#tab indent
for i in range(0, 100):
  print(i, end="\t")

#vertical tab
for i in range(0, 100):
  print(i, end="\v")


We can turn the colors on and off different bits of the code by using end. Remove the previous code from your main.py file and try this out.
print("If you put")
print("\033[33m", end="") #yellow
print("nothing as the")
print("\033[35m", end="") #purple
print("end character")
print("\033[32m", end="") #green
print("then you don't")
print("\033[0m", end="") #default
print("get odd gaps")

Let's concatenate that same print statement:

print("If you put", "\033[33m", "nothing as the", "\033[35m", "end character", "\033[32m", "then you don't", "\033[0m", "get odd gaps", end="")

Now you may notice that we are getting weird double spaces in between the different sections. Let's fix that!
sep Arguments 



Take this same code and change end to sep (short for separator) and add a space at the end of each string. What happens

print("If you put ", "\033[33m", "nothing as the ", "\033[35m", "end character ", "\033[32m", "then you don't ", "\033[0m", "get odd gaps ", sep="")

That GIANT white cursor
we could turn that off! It is just a sneaky print command.
import os, time
print('\033[?25l', end="")
for i in range(1, 101):
  print(i)
  time.sleep(0.2)
  os.system("clear")

Coding Challenge :
Write a subroutine that writes text in color. All it will do is print out the text in that color and turn the color back to normal when it's finished.
Control end and sep so there are not random symbols or spaces.

def newPrint(color,word):
  if color == "Red":
    print("\033[0;31m",word,sep="",end="")
  elif color == "pink":
    print("\033[0;34m",word,sep="",end="")
  elif color == "cyan":
    print("\033[0;36m",word,sep="",end="")
#return (color, word)

print("super Subroutine")
print("with my ",end="")
newPrint("Red","new Programme ")
newPrint("pink","I can just call red")

No comments:

Post a Comment

  2D Dictionaries Remember that dictionaries are very similar to lists, except that they store data as key:value pairs. The value is what it...