Skip to main content

Python Tutorial 4.5 - String Operations in Python

Welcome to the “String Operations in Python” tutorial!


Overview

Strings are immutable sequences of characters with many built‑in methods for manipulation.

Creating Strings

greeting = "Hello, World!"

Common Operations

  • Concatenation and Repetition:
    "Hi " + "there"     # "Hi there"
    "la" * 3            # "lalala"
    
  • Indexing & Slicing:
    greeting[0]         # "H"
    greeting[7:12]      # "World"
    
  • String Methods:
    greeting.lower()    # "hello, world!"
    greeting.upper()    # "HELLO, WORLD!"
    greeting.split(",") # ["Hello", " World!"]
    greeting.strip("!") # "Hello, World"
    
  • Formatting:
    name = "Alice"
    age = 30
    print(f"{name} is {age} years old.")
    

Key Takeaways

  • Strings are powerful and support rich methods.
  • They are immutable—operations return new strings.
  • f‑strings provide concise formatting.

Happy coding!

Comments