Learn to Code via Tutorials on Repl.it!

← Back to all posts
rAiNbOW TeXt
ThisUserTaken (231)

This is just a short tutorial on how to make a cool text!
we will need to import colored from termcolor for colored text. We will also need to import random, sys, and time for random colors, and pausing in between text to make it cool! Let's start!
First, I did:
color_list = ['magenta', 'red', 'yellow', 'blue', 'green', 'cyan', 'white']
This will create a color list for the colors from termcolor.
Next is the rainbow_text function!
def rainbow_text(x): for i in range(len(x)): random_color = random.choice(color_list) print(colored(x[i], random_color), end ="") time.sleep(0.04) sys.stdout.flush() print("")
We create a function called rainbow_text.
It has a parameter x which is the word!
Next, there is a for loop. I used this so it will only run (the amount of letters in the word) times. You'll see why!
In the for loop, it says random.choice(color_list). It will get a random color from the color list and save it as a variable.
The next step is kinda big and confusing. Let's break it down.
print(colored("Hello world", 'blue') will print Hello World in blue text. We are printing colored(x[i], random_color). Let's break this down again! x[i] means the word[index]. i is increasing every time the loop runs. So, if I said rainbow_text("nice"), it will print "n" in the random color. As we print(colored("n", 'random_color (which is the random color)). Time.sleep(0.04) will pause for a while. Sys.stdout.flush() will be needed to do time.sleep. Sadly, I don't know why, but I will research and tell you! Dont forget there was ,end = "". This is very important! Print prints a new line and moves the cursor to the next line, right? But if you put ,end = "" , it won't do that! This whole thing in the loop will repeat the (amount of letters in the word) times, so it prints each letter in the word and pauses! next is print(""). that is just used to print a new empty line.
Yay! We finished the hard part! Next up, there's a while loop. We do this if the user wants to keep doing rainbow text! x will be the variable that takes in their word and saves it as x. Finally, we do rainbow_text(x). Rainbow_text takes in the parameter, x and it does the process to that word! Yay! Done! If you have any issues, please comment. If something is unclear, please tell. If you liked this tutorial, please upvote! Thank you!

Commentshotnewtop
ZDev1 (695)

This is amazing 👍