Python Tutorials

Choose a lesson to begin

String Methods

Strings in Python come with built-in methods that let you transform and manipulate text. Think of them as tools for working with words!

What Are Methods?

Methods are functions that belong to a specific data type. For strings, you use a dot (.) after the variable name:

text = "hello"
result = text.upper()

Case Conversion

.upper() - Make Uppercase

message = "hello"
print(message.upper())  # HELLO

.lower() - Make Lowercase

message = "HELLO"
print(message.lower())  # hello

Use case: Comparing user input

answer = "YES"
if answer.lower() == "yes":
    print("You said yes!")

Text Replacement

.replace(old, new) - Replace Text

sentence = "I love fish"
new_sentence = sentence.replace("fish", "cats")
print(new_sentence)  # I love cats

Important: .replace() creates a new string, it doesn't change the original:

message = "Hello World"
new_message = message.replace("World", "Python")
print(message)      # Hello World (unchanged!)
print(new_message)  # Hello Python (new string!)

Whitespace Removal

.strip() - Remove Leading/Trailing Spaces

messy = "   hello   "
clean = messy.strip()
print(clean)  # hello

Real use: Cleaning user input

username = input("Enter username: ")
username = username.strip()  # Remove accidental spaces
print(username)

Chaining Methods

You can use multiple methods in sequence:

text = "  python  "
result = text.strip().upper()
print(result)  # PYTHON

How it works:

  • text.strip()"python"
  • "python".upper()"PYTHON"
  • Real-World Examples

    Clean and Format User Input

    name = "  Alice SMITH  "
    clean_name = name.strip().lower()
    print(f"Welcome, {clean_name}!")

    Fix Capitalization

    city = "copenhagen"
    proper_city = city.upper()
    print(proper_city)  # COPENHAGEN

    Search and Replace

    template = "Hello NAME, welcome to CITY!"
    personalized = template.replace("NAME", "Vidar")
    personalized = personalized.replace("CITY", "Copenhagen")
    print(personalized) # Hello Vidar, welcome to Copenhagen!

    Clean File Names

    filename = "My Document.txt"
    clean_filename = filename.replace(" ", "_").lower()
    print(clean_filename)  # my_document.txt

    Common Patterns

    Normalize text for comparison:

    user_input = "  YES  "
    if user_input.strip().lower() == "yes":
        print("Confirmed!")

    Format display text:

    product = "laptop computer"
    display = product.upper()
    print(f"Product: {display}")

    Clean and validate:

    email = "  User@Example.COM  "
    clean_email = email.strip().lower()
    print(f"Email: {clean_email}")

    Method Reference

    | Method | What It Does | Example | |--------|-------------|---------| | .upper() | ALL CAPS | "hi".upper()"HI" | | .lower() | all lowercase | "HI".lower()"hi" | | .strip() | Remove spaces from ends | " hi ".strip()"hi" | | .replace(old, new) | Replace text | "hi".replace("h", "b")"bi" |

    Try It Out!

  • Create a message and convert it to uppercase
  • Take a sentence and replace one word with another
  • Clean up messy input: " PYTHON ""python"
  • Chain methods: take " hello world " and make it "HELLO WORLD"
  • Create a mad-libs style program using .replace()
  • Important Notes

    Next Steps

    Ready to work with collections of data? Learn about Lists and For Loops next!

    🐍 Python Runner