LING 398
Elsayed Issa
Jingying Hu
Spring 2026

Lecture Plan (75 minutes):¶

  1. Questions from last time.

  2. Python Basics

    • Strings
    • Lists
  3. Questions

1. Strings

  • The string type is used to store characters.

  • Quotes are used with strings as follows:

1) single quotes: 'Hello Python' 2) double quotes: "Hello Python" 3) triple quotes: """Hello Python""" or '''Python Python'''

In [ ]:
my_string = "Hello Python"
my_string
In [ ]:
my_string = "السلام عليكم"
my_string
  • Python uses UTF-8 unicode by default.

  • The built-in function str is used used to identify the type of the string and convert objects to strings.

In [ ]:
# 1) identify the type of the value stored in the variable called int_number (we ask here if the type of value is int or not)
print(isinstance(my_string, str))

# 2) convert 10 to string
example = 10
print(type(10))
zz = str(example)
type(zz)
  • IMPORTANT: strings are immutable which means that once a string is created, it cannot be changed
In [ ]:
"what's your name?"
In [ ]:
x = """what's your name?
I am 20 Years old.
"""
x
In [ ]:
'what\'s your name?'

Operations on Strings:¶

Indexing and Slicing¶

  • Indexing means accessing individual items from a sequence by specifying the index of that item.
P Y T H O N
0 1 2 3 4 5
-6 -5 -4 -3 -2 -1
  • we have positive and negative indexes.

  • strings are sequences of characters and can be sliced.

  • Slicing syntax is as follows seq[start, stop, step]

In [ ]:
# get index 0 which will be the P letter - positive
word = "PYTHON"
word[0]
In [ ]:
# get index 3 which will be the H letter - positive
word = "PYTHON"
word[3]
In [ ]:
# get index -1 which will be the N letter - negative
word = "PYTHON"
word[-1]
In [ ]:
# slicing with step
word = "PYTHON"
word[0:4:1]
In [ ]:
# get all
word = "PYTHON"
word[:]
In [ ]:
# get what's after index 2 - note that index 2 is included.
word = "PYTHON"
word[2:]
In [ ]:
# get what's before index 2 - note that index 2 is NOT included.
word = "PYTHON"
word[:2]
In [ ]:
# reverse the order using negaive step
word = "PYTHON"
word[::-1]
In [ ]:
# use negative to get the last 3 letters
word = "PYTHON"
word[-3:]
In [ ]:
# using the `slice()` built-in function
word = "PYTHON"
word[slice(0,2,1)]
In [ ]:
# Palindrome
#racecar

#radar

our_word = "dood"
# if the given word is equal to the reverse of the given word, print "It is a Palindrome"
if our_word == our_word[::-1]:
    print("It is a Palindrome")
# Otherwise, print "Give me another word"
else:
    print("Give me another word")

Create newline \n¶

  • means a new line
In [ ]:
sentence = "I ate a good lunch.\n I will eat everyday."
print(sentence)

Tab character \t¶

  • renders a tab character
In [ ]:
sentence2 = "I ate a good lunch\t\t\t. I will eat everyday."
print(sentence2)

Quotes¶

In [ ]:
x = """ I am a string.
I am part of the same string.
    me... too!"""
x

Some string methods:¶

  • capitalize()
  • lower()
  • upper()
  • split()
  • startswith()
  • endswith()
  • format()
  • isdigit()
  • join()

join()¶

In [ ]:
# Example 1
x1 = "Python"
x2 = list(x1)
x2
In [ ]:
"".join(x2)

capitalize()¶

In [ ]:
x1 = "cat".capitalize()
print("Capitalizes the first letter:", x1)

upper()¶

In [ ]:
x2 = "cat".upper()
print(x2)

Format Strings:¶

  • We use the format method to replace placeholders with values

First approach¶

In [ ]:
# we have the following two variables:
name = "John"
subject = "linguistics"

"{name} studies {subject}.".format(name=name, subject=subject)
In [ ]:
"{} loves {}.".format("John", "Mary")
In [ ]:
# padding with spaces
"{item:>8}".format(item="stew")

Second approach¶

  • f-string
In [ ]:
# we use f-string
f"{name} studies {subject}."

2. Lists

  • Lists allow you to store other sequences of objects.

  • We use square brackets for lists []

In [ ]:
# store numbers
list_of_numbers = [1,2,3,4]
list_of_numbers
In [ ]:
# store words
list_of_words = ["I", "am", "happy"]
list_of_words
In [ ]:
# or both
store_both = ["I", "am", 1, 6]
store_both
In [ ]:
# you can add other objects such as None type and boolean
["I", "am", 1, 6, True, False, None, 4.5, [1,2,3]]
  • The built-in function list is used used to identify the type of the list and construct any empty list.
In [ ]:
# 1) declare a variable called `my_list` and store a list of 1,2,3
my_list = [1, 2, 3]
# call the `type` method to find the type of the my_list
type(my_list)
In [ ]:
# 2) use `isinstance`
isinstance(my_list, list)
In [ ]:
# what is the type of "claire"
type("Claire")
In [ ]:
type(["Claire"])
  • construct an empty list: using [] or list
In [ ]:
empty_list = []
empty_list
In [ ]:
empty_list.append("John")
empty_list
In [ ]:
empty_list.append("Johnny")
empty_list
In [ ]:
another_empty_list = list()
another_empty_list
  • NOTE: list() can be used to list out other sequences. We will discuss this later, but here is an example
In [ ]:
list("Purdue is amazing")

Ordering matters:¶

In [ ]:
# A list's ordering matters
[1, 2, 3] == [1, 3, 2]
In [ ]:
[1, 2, 3] == [1, 2, 3]

Indexing and Slicing:¶

  • It follows the same concept as shown when we studied strings
In [ ]:
# get an item by a postive index
colors = ["red", "green", "blue", "black"]
colors[2]
In [ ]:
# get an item by a negative index
colors = ["red", "green", "blue", "black"]
colors[-1]
In [ ]:
# get a subsequence by identifying start-index and stop-index
colors = ["red", "green", "blue", "black"]
colors[0:3]
In [ ]:
# get a subsequence after 2 index 
# (Notice the 2 index will be included)
colors = ["red", "green", "blue", "black"]
colors[2:]
In [ ]:
# get a subsequence before 2 index 
# (Notice the 2 index will NOT be included)
colors = ["red", "green", "blue", "black"]
colors[:2]
In [ ]:
# get a subsequence before -2 index 
# (Notice the 2 index will be included)
colors = ["red", "green", "blue", "black"]
colors[-2:]
In [ ]:
# get a subsequence before 2 index 
# (Notice the 2 index will NOT be included)
colors = ["red", "green", "blue", "black"]
colors[:-2]
In [ ]:
# Now if we can use seq[start, stop, step]
# colors = ["red", "green", "blue", "black"]
# colors[0:2:1]
In [ ]:
# reverse
colors = ["red", "green", "blue", "black"]
colors[::-1]

Some Important Methods¶

append()¶

In [ ]:
# We have a list of cars
cars = ["Honda", "BMW", "Chevrolet"]
# append Toyota to this list
cars.append("Toyota")
# print the updated list
cars

extend()¶

In [ ]:
cars = ["Honda", "BMW", "Chevrolet"]
cars2 = ["Toyota", "Ford", "Cadillac"]

cars.extend(cars2)
cars
In [ ]:
cars.pop()
In [ ]:
cars
In [ ]: