String Methods

Common string manipulation operations

#strings #methods #text-processing

String Methods

Essential string methods for text manipulation.

Case Conversion

text = "Hello World"
print(text.upper())      # HELLO WORLD
print(text.lower())      # hello world
print(text.title())      # Hello World
print(text.capitalize()) # Hello world
print(text.swapcase())   # hELLO wORLD

Searching and Testing

text = "Python Programming"
print(text.startswith("Py"))    # True
print(text.endswith("ing"))     # True
print(text.find("Pro"))         # 7
print(text.count("P"))          # 2
print("123".isdigit())          # True
print("abc".isalpha())          # True

Splitting and Joining

# Split and join
sentence = "one,two,three"
words = sentence.split(",")      # ['one', 'two', 'three']
rejoined = "-".join(words)       # one-two-three

# Strip whitespace
text = "  hello  "
print(text.strip())   # "hello"
print(text.lstrip())  # "hello  "
print(text.rstrip())  # "  hello"

Discover another handy tool from EditPDF.pro