Module 1 Section 2- Types & Variables - What is a String?

 

Photo by Shahadat Rahman on Unsplash

June 2, 2018 

Introduction to Python: Absolute Beginner 

Unit 1 Section 2 

Strings: 

Strings are sequence of one or more characters and symbols includes: 

1.       A to Z (capitalized Letters),

2.       a to z (lower case letters),

3.       0 to 9 (numbers), and

4.       #, %, @, *, &, /, +, =, !, $, and etc.(are variety of punctuations (symbols) other than letters and numbers). 

If we view our computer keyboard, our keyboard contains all characters and symbols. 

In a later tutorial, we will see how to not printable such as, a newline character. 

Using Single or Double Quotes: 

print statement strings can either be single (‘  ‘) or double (“  “) quotes. 

For example:     print(‘ string go in single quotes ‘)

                            or

                            print(“ double quotes “)

                                      and much be in matching quotes. 

If we mismatch our print statement (‘       “) quotes, we can see how Python echoes the single and double quotes together an syntax error will occur message. 

·         A syntax error message tells us, we have not use proper Python strings format. 

Integers: 

Integers are data types and whole numbers (0 to 9) use for mathematical operations includes: 

1.       counting (integers mathematical operations for example, adding together positive or negative or combination of both numbers) and etc.,

2.       positive (+),

3.       negative (-), and

4.       zero (0) numbers. 

We recognized in this Python tutorial strings represents A to Z and digits from zero (0) to nine (9) however, the difference between strings and integers are string numbers (0 to 9) represents a picture print or image of that particular number[s] for example: 

The string character representation for number three (3) is view as, a picture print or image vice verses in the actual integer number for three (3) is use as a mathematical operation. 

So, 

the differences between strings and integers are quotes and no quotes procedures for example: 

in code module, the string double quotes:  “-123” echo  ‘-123’ 

or 

in code module, the integer echoes no quotes form a mathematical operation: -123 echo -123. 

However, in the print statements in strings and integers numbers behaves differently, in the code cells run module for string number using double quotes: 

·         string numbers will echo, as no quotes output

for example, print(“2017”) echoes, 2017                               

and 

in the code cells run module, for integers enclosed no quotes only open and close parentheses 

·         integer numbers will echo, no quote output

for example, print(2017) echoes, 2017. 

If we add a letter to an integers, Python will generate a syntax error message because integers are mathematical computations only and not a characters’ strings. 

We do not mismatch or use strings characters with integers, and integers are enclosed with parentheses inside with no quotes. 

Variables: 

A variable is a reserved memory locations to store values of an object for example: 

·         student_name is a variable that holds the object value of Carl Matthews. 

student_name = “Carl Matthews”

        (variable)        =       (object value) 

Variables have three (3) functions includes: 

1.       reserved memory,

2.       store location, and

3.       holds objects or values.

*

We think, as an algebra equations assigned values mathematical computations, in X is equal to 5 (x = 5), and other equations. 

Python does the small mathematical logics computations, we are using variables, so, we can reuse our codes outputs and we can also assigned different values (objects) to that variables. 

Remember, variable can hold strings and literal variable can be use instead of string characters. 

·         A variable can be assigned strings not just in mathematical operations for example: 

student_name is a memory storage location assigned to a object [value] enclosed inside parentheses then we can call back on string in a code cell runs echo procedures. 

We can use print statement to print only string characters, we have learned in our previous tutorial for example. 

print(“Joana Dias”) 

However, using variable, we can add a different variable (student_name) to the print statement where as, other codes change the values of a variable (student_name). 

We can reassign variable (student_name) to another object [value] Colette Browning for example: 

print(Student_Name)

Student_Name = "Colette Browning"

print(Student_Name) 

then 

echoes 

Joana Dias

Colette Browning 

The first print statement ran in Python echoed, Joana Dias as a object [value] of a variable (student_name) then we can reassign a new variable to Colette Browning. 

If we go to the cell that contain Joana Dias (variable) 

student_name = "Joana Dias" 

Then 

rerun (run cells) the cell in Python we can reinitializes the variable in the current print statement cell: 

student_name 

and also, we can check for further object [value] associated with that particular variable (student_name) in Colette Browning 

student_name = "Colette Browning" 

then rerun the code 

the print statement will change the echo output to Colette Browning and so on… 

 

Task 3 – Question: 

In the cell above, why did the variable have a value to print before it was assigned? 

The variable (current_msg) has to be rerun in the print statement cell for example: 

message_msg = “June 2, 2018” 

then initialize for variable (message_msg) results, again. 

Data Type: 

Data Type are classification of data which tells the interpreter, how the programming language intends to use the cell code. 

·         Strings and Integers object or value are data types.

·         Variables in Python can change data types. 

Integers has three types of defined numbers: 

1.       Integers Number (int) for example a = 5, 5 is of type <class ‘int’>,

2.       Floating Point Numbers (float) for example a = 2.0,  2.0 is of type <class ‘float’>, and

3.       Complex Numbers (Complex) for example a = 1+ 2j, (1+2j) is complex number? True, class in Python. 

We are going to assign a variable to string for example: 

# String Variable

student = "Joana Dias" <Joana Dias is a String enclosed by double quotes>

student 

then run cells that echo in single quote: 

‘Joana Dias’ 

Likewise, assigning a variable to an integers for example: 

# Change to integer Variable

student = 123456

student 

Then run cells that echo in no quote: 

123456. 

Python allows variable to change data type (object or value) for example: 

student = "Diaz-J123456"

student 

Then run cells that echo in single quote: 

‘Diaz-J123456’ 

You have to close attention between strings and integers programmatic logic. 

We can also, print statement variable data types. 

# Variable String Print

print(student) 

then run cells that echo in no quote 

Diaz-J123456 

If we go to the cell that contain 123456 (variable) 

student = 123456 

Then 

rerun (run cells) the cell in Python, we can reinitializes the variable in the current print statement cell: 

print(student) 

resulting in: 

123456 

Q?: What is the difference between 123 and '123'?

A: A String type is not a number, even if it is all numeric digits

·         in Python code a number can be assigned to a variable: x = 31

·         a string of digits can be assigned to the variable id  --> id = "001023"  the quotations, mean the number symbols should be stored in the variable as type string, meaning the digits "001023"  are treated as text.

The print() function can print Integer or string values:

print(123) #integer, with numeric value

print("123") #string, represents text characters

 

 

 

 

 

 

 

 

 


Comments