String Methods in Python
Strings come with many built-in methods that help you manipulate and check text. These methods return new strings – they don't change the original (strings are immutable).
We'll go through the most common ones with explanations and examples you can run right away.
Changing Case
These methods convert the string to uppercase, lowercase, title case (first letter of each word capitalized), or capitalize the first letter.
Trimming Whitespace
strip() removes whitespace from both ends, lstrip() from the left, and rstrip() from the right. This is useful when cleaning user input.
Replacing Text
replace() substitutes text. By default it replaces all occurrences, but you can limit the number of replacements.
Splitting and Joining
split() turns a string into a list by separating at a delimiter (default is whitespace). join() does the opposite – combines a list into a string using a separator.
Checking Content
These methods return True or False to check if a string starts/ends with something or contains only certain characters.
Finding Positions
find() returns the index of the first occurrence or -1 if not found. index() does the same but raises ValueError if not found.
Quick Quiz
Wir haben die Materialien überprüft, dennoch können Fehler vorkommen. Der Inhalt dient ausschließlich Bildungszwecken, daher verwende ihn auf eigene Verantwortung und überprüfe ihn bei Bedarf mit anderen Quellen.
✨ Frag Lara — deine KI-Lernpartnerin
Entsperre personalisierte Lernunterstützung. Lara kann Lektionen erklären, Themen zusammenfassen und deine Lernfragen beantworten — verfügbar ab dem Go-Tarif.
Lara hilft dir, schneller zu lernen — exklusiv für ReadyTools Go-, Plus- und Max-Mitglieder.
