Types represent various ways of representing data in Python.
Example: Integer is a type.
We will focus on Strings in this discussion.
Definition: A string is a sequence of characters.
Characters can be numbers, letters, or symbols placed one after another.
Used for defining names, identifiers, and any textual representation.
String Literals: A sequence of characters enclosed in single (' ') or double (" ") quotes.
There is no distinction between single and double quotes in Python as long as consistency is maintained.
String as a Sequence Type: Strings are a sequence type, meaning you can access characters by their position.
Example: The string "Trish" has characters indexed starting from zero (T=0, r=1, i=2, s=3, h=4).
Zero-Based Indexing: Indexing in Python starts at zero.
Visualization:
For the string "Trish":
Indexes: 0 - T, 1 - r, 2 - i, 3 - s, 4 - h.
Filling in Blanks Exercise:
Create sentences by substituting placeholders with input values (e.g., relative, food).
Example sentence: "My aunt says eating chile will make one excellent."
Input: Use the input()
function to get user inputs to replace placeholders.
Print Statement:
Use print()
to output the final string with values substituted:
Separated by commas for literals and variables.
Using len()
Function:
Calculates the length of a string.
Example: len("George V")
returns the length of the string as an integer.
Backslash as Continuation Character:
Allows splitting strings across multiple lines.
Example: George_V = "George \ V"
creates a single string.
Access characters via square brackets []
based on their index.
Positive Indexing:
alphabet[0]
returns 'A', alphabet[1]
returns 'B', etc.
Negative Indexing:
alphabet[-1]
returns the last character, alphabet[-2]
the second last, and so forth.
Strings are immutable: Individual characters cannot be changed.
Example of Error: Attempting to assign a new character to a specific index will cause an error.
Using +
Operator:
Combines two strings.
Example: "Mars" + " has "
results in "Mars has".
Formatted Strings:
Begin with the letter 'F' followed by quotes. Variable placeholders are included in curly braces {}
.
Example: f"Val: {value}"
replaces {value}
with the actual value.
Placeholder Expressions: Formatting can also evaluate expressions with the equal sign in formatted strings.
Definition: A list is a container that holds multiple items.
Created using square brackets []
, and items are separated by commas.
Example: prices = ['$20', 3.0, 5]
is a list with elements of different types.
Indexing in Lists: Similar to strings, accessing elements starts from index 0.
Updating List Values: Unlike strings, elements in lists can be modified.
Common Methods:
append(): Adds a new element to the end of the list.
pop(): Removes an element by index and returns its value.
remove(): Removes an element by value and does not return anything.
Sequence Type Methods: Functions that operate on sequence types include len()
, min()
, max()
, sum()
, etc.
Concatenation of Lists: Two lists can be combined by using the +
operator, effectively joining them into a new list.