Python String Methods Unveiled: Part Three

Ernest Asena
3 min readSep 18, 2023

--

python strings

Greetings once more, valiant Python explorers! 🎓 Welcome to the thrilling conclusion of our epic journey through Python’s wondrous array of string methods. In this third and final installment, we’ll explore the last 10 methods on our list, each a gem in the treasure trove of string manipulation. Let’s dive straight in …

26. str.ljust(width, fillchar) - The Left Aligner

When it’s time to align your string to the left within a given width, str.ljust() emerges as the left aligner. It fills any remaining space with the specified fill character.

text = "Python"
left_aligned = text.ljust(10, "-")
print(left_aligned)
# Output: "Python----"

27. str.rjust(width, fillchar) - The Right Aligner

Conversely, if right alignment is your goal, str.rjust() stands as the right aligner. It pads the left side with the fill character to reach the specified width.

text = "Python"
right_aligned = text.rjust(10, "-")
print(right_aligned)
# Output: "----Python"

28. str.center(width, fillchar) - The Center Stage Setter

For text that demands a grand entrance at the center stage, str.center() takes the spotlight. It centers your string within the specified width and fills the margins with the fill character.

text = "Python"
centered = text.center(10, "-")
print(centered)
# Output: "--Python---"

29. str.expandtabs(tabsize) - The Tab Expander

In the realm of whitespace manipulation, str.expandtabs() excels as the tab expander. It replaces tab characters with spaces, aligning text to the specified tab size.

text = "Code\tPython\tLearn"
expanded = text.expandtabs(4)
print(expanded)
# Output: "Code Python Learn"

30. str.swapcase() - The Case Chameleon

When your text craves a change of case, str.swapcase() is the case chameleon. It toggles the case of each character, transforming uppercase to lowercase and vice versa.

text = "PyThOn"
swapped = text.swapcase()
print(swapped)
# Output: "pYtHoN"

31. str.encode(encoding) - The Encoder

In the realm of character encoding, str.encode() plays the role of the encoder. It converts your string into a bytes object using the specified encoding.

text = "Python"
encoded = text.encode("utf-8")
print(encoded)
# Output: b'Python'

32. str.startswith(prefix, start, end) - The Prefix Pioneer

To explore whether your string begins with a specific prefix within a given range, str.startswith() steps forward as the prefix pioneer. It returns True or False accordingly.

text = "A grand journey awaits."
starts_with_grand = text.startswith("grand", 2, 10)
print(starts_with_grand)
# Output: True

33. str.endswith(suffix, start, end) - The Suffix Sentinel

Similarly, str.endswith() becomes the suffix sentinel when you need to check if your string ends with a particular suffix within a specified range.

text = "To be continued..."
ends_with_ellipsis = text.endswith("...", 0, 20)
print(ends_with_ellipsis)
# Output: True

34. str.rindex(substring) - The Rightmost Indexer

To discover the index of the rightmost occurrence of a substring within your string, str.rindex() acts as the rightmost indexer.

message = "Searching for the last word."
index = message.rindex("the")
print(index)
# Output: 14

35. str.rsplit(sep, maxsplit) - The Rightmost Splitter

When the need arises to split your string from the right side, str.rsplit() serves as the rightmost splitter. It divides the string into substrings, starting from the right.

text = "The Pythonic way to code"
split_words = text.rsplit(" ", 3)
print(split_words)
# Output: ['The Pythonic', 'way', 'to', 'code']

With these final 10 methods, our exploration of Python string methods is now complete. Armed with this knowledge, you are well-equipped to master the art of string manipulation and wield these tools with precision.

We hope you’ve enjoyed this grand adventure, and we wish you continued success in your Python endeavors!

Happy Coding!!

--

--