Create cleaned, formatted, and validated strings using powerful built-in methods.
Python strings are immutable, which means you cannot change them in-place. Instead, string methods like strip(), upper(), lower(), replace(), split(), and join() create and return new modified strings.
split() and join().strip(), lstrip(), rstrip() remove spaces.upper(), lower(), capitalize(), title().replace(old, new) substitutes one substring with another.split() turns a string into a list; join() combines list items back into a string.find(sub) gives the index of a substring (or -1 if not found).startswith(), endswith(), isalnum(), isalpha(), isdigit() validate content.General pattern for using string methods:
original = "hello world"
modified = original.upper() # method call on the string object
print(original) # hello world
print(modified) # HELLO WORLD
The variable original is not changed. The result of the method is stored in a modified variable (or printed directly).
Use trimming and case methods to remove extra spaces and adjust capitalization.
text = " Hello World "
print(text.strip()) # "Hello World" - removes leading/trailing spaces
print(text.upper()) # " HELLO WORLD "
print(text.lower()) # " hello world "
print(text.replace("World", "Python")) # " Hello Python "
Break a string into a list of words and then join them with a custom separator.
text = "python programming"
print(text.capitalize()) # Python programming
print(text.title()) # Python Programming
words = text.split()
print(words) # ['python', 'programming']
joined = "-".join(words)
print(joined) # python-programming
Find substrings and verify what kind of characters a string contains.
text = "Python3"
print(text.find("3")) # 6
print(text.startswith("Py")) # True
print(text.isalnum()) # True
print(text.isalpha()) # False
strip() removes spaces only from the beginning and end, not in the middle.upper() and lower() convert all letters, but keep spaces unchanged.replace("World", "Python") swaps every occurrence of "World" with "Python".capitalize() capitalizes only the first character of the whole string.title() capitalizes the first character of each word.split() without arguments splits on any whitespace and returns a list of words."-".join(words) joins list items with "-" between them.find("3") gives the index position of "3" or -1 if not found.startswith("Py") checks whether the string begins with that substring.isalnum() is True if all characters are letters or digits and there is at least one character.isalpha() is False for "Python3" because of the digit "3".strip() (or lstrip()/rstrip()) to clean user input before processing.replace() and upper() return a new string.text = text.strip().split() and join() for flexible text processing (for example, replacing all spaces with commas or hyphens).replace() instead of manually changing characters one by one.text[0] = "P"); this will raise an error.strip()."Python" with "Coding" in a string using replace().split() and join them using commas with join()."Hello" or ends with "World" using startswith() and endswith().