from termcolor import colored
print (colored("Hello World!","red"))
print (colored("Hello World!","cyan"))
print (colored("Hello World!","magenta"))
print (colored("Hello World!","green"))
Text colors:
grey
red
green
yellow
blue
magenta
cyan
white
Text highlights:
on_grey
on_red
on_green
on_yellow
on_blue
on_magenta
on_cyan
on_white
import time
time.sleep(2) >>> delay by two seconds
OR
from time import sleep
sleep(2) >>> delay by two seconds
This call will delay the next step of the program by the number inside the sleep function
Example #1
# import modules: sys.stdout.write(), sys.stdout.flush()
from sys import stdout
# import modules: time.sleep()
from time import sleep
# a user-defined function called 'write' to have a typewriter effect
# you don't need to know how it works but you can still use it if you understand
# (a)what it does with the data passed into the function and
# (b) how to use the function properly (look at the examples below)
def write(whatWillIPrint,t):
for i in whatWillIPrint: # loops through each letter of the word(s) we pass into the function
stdout.write(i)
stdout.flush()
sleep(t) # pause in typing each letter
write("#TypewriterEffect!\n\n",0.2) #0.2 second time delay
write("Welcome to Digital Technologies today on a Friday!\n\n",0.1) #0.1 second time delay
for x in range (10):
write(1)
write("...\n",0.2) #0.2 second time delay
write("Hello. Please enter your name.")
userName = input(">>> ")
#concatenate userName with the words "Hello" and "welcome to Python"
userName = "Hello "+ userName + " welcome to Python."
write(userName,0.1) #0.1 second time delay
Timing using built in 'Python time'.
# import time library
import time
# declare a variable for the 'start' of your timing (beginning of game or question)
start = time.time()
# do something (e.g. get the answer for your question)
count = 1
while count <= 10:
print(count)
count += 1
time.sleep(1)
# declare a variable for the 'end' of your timing
end = time.time()
# calculate the time difference, and round the number
totalTime = end - start
totalTime = round(totalTime,2)
print("Total time taken = " + str(totalTime))
Timing using minutes + seconds
from datetime import datetime
from datetime import timedelta
#get current time
start = datetime.now() #get current time in days and hours
#function to convert time to a minute and second format
def timeformat(secs):
m = round(secs/60)
s = secs%60
if s>9:
print(str(m) + ":" + str(s))
else:
print(str(m) + ":0" + str(s))
#call function
timeformat(start.seconds)
- http://www.pythonforbeginners.com/basics/string-manipulation-in-python
Accessing individual Characters from a variable
word = "Hello World"
letter=word[0]
>>> print letter
H
Other options:
word = "Hello World"
print word[0] #get one char of the word
print word[0:1] #get one char of the word (same as above)
print word[0:3] #get the first three char
print word[:3] #get the first three char
print word[-3:] #get the last three char
print word[3:] #get all but the three first char
print word[:-3] #get all but the three last characters