In the last article you learned how to assign and reassign values to variables.
In this article you'll learn about the rules and guidelines for choosing variable names. You'll also learn about the string data type.
Variable Naming Conventions
As mentioned in the last article, a variable is a label that references a certain value.
There are rules and guidelines for naming variables. Breaking some of these rules might cause errors in your program or make your programs difficult to read and understand.
A program that is difficult to understand would be difficult to maintain.
Keep these in mind when choosing variable names:
1. Variables cannot start with numbers
Variables can contain only letters, numbers and underscores but cannot start with numbers.
Some valid variable names are message1, message_1, _message_1 and so on.
1_message is an invalid variable name.
Please note that variable names can contain only underscores (as indicated above) but not other symbols. #message, message!, messa!ge are invalid variable names.
2. Variables names cannot contain spaces
"message 1" and "my message" would throw errors. One way to do this is to separate words with underscores. message_1 and my_message are valid variable names.
3. Reserved words cannot be used as variable names
Python reserved words are words that have predefined meaning in python. These words have been used to develop instructions in the Python.
Some reserved words are and, if, global, else, if, print, input, def e.t.c.
These names cannot be used as variable names.
4. Variable names should be short and descriptive
Short and descriptive variable names make programs readable and easy to understand for users of the program. They also make programs easily maintainable for programmers.
number_of_students_in_class can be written as class_size.
Going against this rule doesn't cause an error but it is one of the conventions for writing clean code.
5. Use lowercase and uppercase letters carefully
The lowercase letter "l" can be confused with number "1".
The uppercase letter "O" can also be confused with number "0".
It can take some time to master these rules but you'll get better as you write more programs.
Avoid Name Errors When Using Variables
message and Message are different variable names.
Creating a variable, message, and using another variable, Message, will throw a NameError.
It is a popular convention to use lowercase letters for variable names.
Using uppercase letters won't throw an error but it is used conventionally for permanent variables.
Permanent variables are variables whose values shouldn't change throughout the execution of a program.
Strings
A string is a series of characters surrounded by single or double quotes. "Hassan", "My name is Hassan" are strings surrounded by double quotes. 'Hassan' and 'My name is Hassan' are also strings but surrounded with single quotes.
This flexibility enables us to use apostrophes and quotes within strings.
"I'm a boy" is a valid string because a single quote is used within double quotes.
'Einstein once said, "Never memorize something that you can look up".' is also valid.
Always ensure to use single quotes within double quotes and vice versa.
If you must go against this rule, ensure to use the escape character, backward slash (\)
The escape character prevents the subsequent quote from terminating the first quote.
Failing to adhere to this rule results in syntax error. Syntax error indicates that python doesn't understand a section of your code.
Adding White Spaces to Strings
We can format strings nicely by using special characters.
Newlines can be added to strings by using the combination "\n"
The "\n" combination in the second line adds a newline after each programming language.
Tabs can be added to strings by using the combination "\t".
The "\t" combination in the second line adds a tab before each programming language
Multiline Strings
We can format the previous example nicely without adding newlines and tabs manually.
We have to surround the string with three single or double quotes.
Basic String Operations
String Methods
A method performs an action on an object.
An object is an instance of a class. We'll cover this in the object oriented programming part of this python series.
String methods are methods that perform certain actions on strings.
Change the case of a string
The title method changes each word to title case (every word begins with a capital letter).
The upper method changes every letter to uppercase.
The lower method changes every letter to lowercase.
Strip white spaces from a string
The lstrip method in the second print statement removes extra whitespace from the left side of a string
The rstrip method removes extra whitespace from the right side of a string
The strip methods removes extra whitespaces from both sides of a string.
Length of a String
The len function allows us to know the number of characters present in a string including whitespaces.
Just like the print function, the len function takes an argument.
String Concatenation
We can join strings together by using the "+" symbol. This process is called concatenation.
As you can see, the concatenation process doesn't add a whitespace between the strings. We always have to add it manually.
Another way to do this is to separate the strings by comma. This automatically adds whitespace(s)
String Repetition
The "*" operator is a multiplication symbol in python. When used on strings it repeats the string for the specified number of times.
Using Variables in Strings
There might be times when we want to include a predefined variable in our string.
We didn't add white spaces separately because they've been added to the end of each string.
There's a better way to do this. F-strings.
F-strings allow us to include multiple variables in a string without breaking them into chunks. The string starts with the letter "f" and variables are put in curly braces.
F-strings are only available to python 3.6 and newer versions.
For python 3.5 and earlier versions, you'll need to use the format method.
The curly braces are filled with variable values in the order specified by the format method.
Summary
- There are certain rules guiding the choice of variable names
- String methods perform certain actions on strings
- F-strings allow us to include variables in strings.
No comments: