1) Printing Information

Perhaps the most basic Python command, the print() command allows users to display information in the console. 

Running the print() command is simple; any information must be written inside a pair of parentheses. Sentences must be written within quotation marks (“) or apostrophes (‘) so that Python knows that the sentence is not code. The example below will print out the phrase “Hello World.” 

To run the program, press the Run button on the top of the screen. 

Variables in Python are bits of information that can be stored within your code. There are 3 basic types of data in Python: strings, numbers, and Boolean values (True or False). Once you have defined a variable, you can print it out in the console. 

Note: In Trinket, strings are red, numbers are blue, and Boolean values are purple. 

You can also combine variables to create sentences, but be sure to convert any number or Boolean value into a string first.

Helpful Commands

Strings can be altered or changed through the use of simple commands. The commands below are useful for formatting phrases and variables (phrase represents any string variable):Note: counting in Python begins with 0. So the first letter of any phrase would be in the 0 position:

phrase.upper(): makes all letters in phrase uppercase
phrase.lower(): makes all letters in phrase lowercase
phrase.isupper(): tests to see if phrase is uppercase; delivers a Boolean value
phrase.islower(): tests to see if phrase is lowercase; delivers a Boolean value
len(phrase): finds the amount of digits or letters in phrase; delivers a number
phrase[x]: prints the letter in the x position in phrase
phrase.index(“x”): finds the location where the letter (in this case x) is located; only gives the first letter, not the total total
phrase.replace(“x”, “y”): replaces x with y wherever x is located in the phrase

Leave a Reply