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'.for i in range(0, 100):
print(i)
Here output as 0,1,2,3,....99 will be printed in a vertical manner .
for i in range(0, 100):
print(i, end=" ")
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
What happens if you add these different options in your second argument? Play around with these options below and see what they do:
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.
#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")
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")
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="")
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