Python Basics Part 7: Keywords, Strings, Debugging, and Package Installation

Python Basics – Part 7: Keywords, Strings, Debugging, and Packages

In this week’s Python training notes, we’ll cover several essential yet often overlooked topics — Python’s reserved keywords, powerful string operations, debugging techniques, and how to install and use external packages. This is the last post for this week, but the series will continue in the coming lessons.

Python Keywords

Python has a set of reserved words that cannot be used as variable names.

import keyword
print(keyword.kwlist)

Example Output:

['False', 'None', 'True', 'and', 'as', 'assert', 'async', 'await', 
 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 
 'except', 'finally', 'for', 'from', 'global', 'if', 'import', 
 'in', 'is', 'lambda', 'nonlocal', 'not', 'or', 'pass', 'raise', 
 'return', 'try', 'while', 'with', 'yield']

There are currently 35 keywords in Python (as of November 2025).

You can also test whether a word is a keyword:

print(keyword.iskeyword("while"))  # True
print(keyword.iskeyword("hello"))  # False

String Indexing and Slicing

Strings are immutable sequences — you can read their parts but not modify them.

Indexing

s = "Hello String"
print(s[0])  # H
print(s[6])  # S

Slicing

s = "0123456789"
print(s[3:7])  # 3456
print(s[:4])   # 0123
print(s[3:])   # 3456789

Negative Indexing

print(s[-1])  # 9
print(s[-2])  # 8
print(s[-7:-2])  # 34567

Step Parameter

print(s[1:8:2])  # 1357
print(s[::-1])   # 9876543210

Use len() for dynamic slicing:

word = "Hello"
print(word[:len(word)//2])  # Hel

String Methods

Strings in Python come with a rich set of built-in methods.

Case Conversion

print("Hello".upper())  # HELLO
print("Hello".lower())  # hello
print("hello world".capitalize())  # Hello world
print("hello world".title())       # Hello World
print("Hello World".swapcase())    # hELLO wORLD

Testing Methods (is...)

These return Boolean values (True/False).

print("asdf".islower())   # True
print("ASDF".isupper())   # True
print("1234".isdigit())   # True
print("Hello".isalpha())  # True
print("Hello123".isalpha())  # False

Checking Start or End

print("Hello World".startswith("Hello"))  # True
print("Hello World".endswith("World"))    # True
print("Hello".lower().startswith("he"))   # True

Replacing Text

print("apple apple apple".replace("apple", "banana", 2))
# banana banana apple

Finding and Counting

text = "aaaaaaaaaaCaaaaCaaaaaaCaaa"
print(text.count("C"))      # 3
print(text.find("C"))       # 10
print(text.index("C"))      # 10
print(text.find("Z"))       # -1 (not found)

Joining and Splitting

Join:

words = ["Today", "is", "a", "beautiful", "day"]
sentence = " ".join(words)
print(sentence)

Split:

date = "20.11.2025"
parts = date.split(".")
print(parts)  # ['20', '11', '2025']

You can also limit the splits:

print("A B C D".split(" ", 2))  # ['A', 'B', 'C D']

Stripping Whitespace

print("   Hello   ".strip())  # Hello
print("abcabcHelloabc".strip("abc"))  # Hello

Debugging in Python

Using Breakpoints in IDEs

In PyCharm or VS Code:

  • Click on the line number to set a breakpoint.
  • Run in debug mode to step through the code.

Example:

x, y, z = 1, 2, 3

for i in range(10):
    x = x + 1
    y = y + x
    z = z * y + x  # Hard to trace without debugging!

print(x, y, z)

Quick Debug Tips

  • Use inline print statements: print(f"i={i}, x={x}, y={y}, z={z}")
  • Or use the built-in breakpoint() (Python 3.7+): breakpoint()

Installing Python Packages

Python has a massive ecosystem of packages you can install with pip.

Three Function Types

  1. Built-in (available by default): print(), input(), etc.
  2. Imported modules: random.randint(), math.sqrt(), etc.
  3. External packages (must be installed): emoji, faker, colorama, etc.

Using pip

CommandDescription
pip listLists all installed packages
pip install <package>Installs a package
pip uninstall <package>Removes a package
pip show <package>Displays details about a package
python -m pip install <package>Alternative method if pip fails

Example: emoji

import emoji

print(emoji.emojize("Python is :thumbsup:", language="alias"))
print(emoji.emojize("Python is fun :red_heart:", variant="emoji_type"))

Output:
👍 ❤️

Example: faker

from faker import Faker

faker = Faker()
print(faker.name())

faker_tr = Faker(["tr_TR"])
print(faker_tr.name())
print(faker_tr.address())
print(faker_tr.sentence())

Example Output:

Ayşe Demir
Bağcılar, İstanbul
Bu güzel bir örnek cümledir.

Example: colorama

from colorama import Fore, Style
print(Fore.RED + "This text is red!" + Style.RESET_ALL)

Checking Installed Packages

pip show emoji

This displays metadata such as version, author, and installation path.

Useful Built-ins

print(dir(str))   # lists all string methods
help(str)         # opens the help documentation

Summary

In this final part, we covered:

  • Python’s reserved keywords
  • String indexing, slicing, and methods
  • The difference between find() and index()
  • Converting strings to and from lists
  • Debugging with breakpoints and breakpoint()
  • Installing and using external packages with pip, including emoji, faker, and colorama