In this post you wil learn how to declare a variable and understand strings(numbers will be covered in the next post)
Variables
First, let's understand the meaning of it in Computer Science, think of it like a box, we will give that box a name for example "message", then we will store in it a word "Hello!", this is a variable, a box where you store in it words, numbers...
Now open the IDLE, File > New File and write the following:
message = "Hello!"
Then save it and give it a name... Run(f5) this program to see what happens.
print(message)Hello!
We've added a Variable named message, it holds the value "Hello!", wich is the information associated with that variable, and when we tried to print it with the Function"print" it showed us its value. Now let's make our program a little bigger...
Write this please:Please Run it(f5) and you should see two lines of output:message = "Hello!"
print(message)
message = "Hello Mr James!"
print(message)You can change the value of your variables in your program at any time.Hello!
Hello Mr James!Naming Variables
-> Variable names contain only letters, numbers and underscores but can't start with numbers.
-> Spaces aren't allowed in variables, but underscores can seperate words.
-> Avoid using Python Keywords as variable names
Strings
Strings are text that might be numbers or letters that are between quotes""."Here is a string"
'Here is another'Changing Case
The "title()" method displays each word in titlecase.name = "james something"
print(name.title())
James Somethingname = "James Something"
print(name.upper())
print(name.lower())
JAMES SOMETHING
james somethingCombining Strings
Run it and it will print the followingfirst_name = "james"
last_name = "something"
full_name = first_name + " " + last_name
print(full_name)We wrote " " so that the first_name and last_name can be seperated.james something
Adding whitespace or tabs to strings
But for newlines we add "/n".>>> print("Pizza")
Pizza
>>> print("/tPizza")
Pizza>>> print("Pizza")
Pizza
>>> print("/nPizza")
PizzaStripping whitespace
To strip white space you declare your variable and add to it .strip().favorite_language = ' python'
favorite_language.strip()
'python'
That is all for today's post see you in the next one!