devnotes.

String Basics and Methods

Strings in Python are a built-in type with a large set of useful methods, and can be written with single, double, or triple quotes.

name = "Rahim"
greeting = 'Hello'
multi = '''Line one
Line two'''            # triple quotes allow multi-line strings

print(name.upper())        # RAHIM
print(name.lower())        # rahim
print(" hi ".strip())      # "hi" — removes leading/trailing whitespace
print(name.replace("R", "K")) # Kahim
print("a,b,c".split(","))   # ['a', 'b', 'c']
print(",".join(["a","b","c"])) # "a,b,c"
print(name.startswith("Ra")) # True

Single and double quotes behave identically in Python (unlike some languages) — the choice is purely stylistic, though it's common to prefer one and switch to the other when the string itself contains that quote character ("it's fine" avoids escaping the apostrophe). Triple-quoted strings (three double-quote characters on each side) span multiple lines and are also used for docstrings — the standard way to document a function or class, placed as the first statement in its body.

String methods never modify the original string — strings are immutable in Python (covered in the next lesson), so .upper(), .replace(), etc. always return a new string, leaving the original untouched. .split() and .join() are the standard pair for converting between a string and a list of strings, and are used constantly when processing text data.

Python String Basics and Methods | DevNotes