Python String Methods Unveiled: Part Two

Ernest Asena
4 min readSep 18, 2023

--

python strings image

Greetings once more, intrepid Python adventurers! šŸ§ Our voyage into the realm of Python string methods continues with Part Two of our epic saga. In this installment, we shall explore the next 15 methods on our list, each a potent tool in the art of string manipulation. Letā€™s dive straight in ā€¦

11. str.split() - The Divider of Words

In the vast sea of text, str.split() emerges as a trusty navigator. It splits your string into a list of substrings, dividing them at whitespace by default or at your command.

sentence = "To be or not to be."
words = sentence.split()
print(words) # Output: ['To', 'be', 'or', 'not', 'to', 'be.']

12. str.join(iterable) - The String Composer

When the time comes to unite the elements of an iterable into a harmonious string, str.join() is the maestro. It composes a string by joining the iterable's components.

words = ["Python", "is", "awesome!"]
sentence = " ".join(words)
print(sentence)
# Output: "Python is awesome!"

13. str.find(substring) - The Seeker of Substrings

In the quest to locate a specific substring within your string, str.find() becomes your trusty guide. It reveals the index of the first occurrence of your sought-after treasure.

message = "A needle in a haystack."
index = message.find("needle")
print(index)
# Output: 2

14. str.rfind(substring) - The Last of the Seekers

For those rare instances when you seek the final occurrence of a substring, str.rfind() arises as your dedicated companion. It unveils the index of the last sighting.

message = "The final word. The very final word."
index = message.rfind("final")
print(index)
# Output: 25

15. str.index(substring) - The Index Detective

Akin to str.find(), str.index() aids in locating a substring. However, it issues an exception if the sought-after substring eludes discovery.

message = "In pursuit of knowledge."
index = message.index("knowledge")
print(index)
# Output: 14

16. str.count(substring) - The Counter of Occurrences

To enumerate the number of times a substring graces your text, str.count() takes center stage. It provides you with a tally of occurrences.

message = "How many times does 'times' appear?"
count = message.count("times")
print(count)
# Output: 2

17. str.isalnum() - The Alphanumeric Judge

In the courtroom of strings, str.isalnum() assumes the role of a fair judge. It determines whether every character in your string is an alphanumeric character.

alphanumeric = "Python42"
is_alphanumeric = alphanumeric.isalnum()
print(is_alphanumeric)
# Output: True

18. str.isalpha() - The Alphabetic Arbiter

For a more focused inquiry, str.isalpha() decides whether every character in your string is a letter of the alphabet.

alphabetic = "Python"
is_alphabetic = alphabetic.isalpha()
print(is_alphabetic)
# Output: True

19. str.isdigit() - The Digit Detector

When numeric scrutiny is paramount, str.isdigit() plays its role as the digit detector. It confirms if all characters are numeric digits.

numeric = "12345"
is_numeric = numeric.isdigit()
print(is_numeric)
# Output: True

20. str.islower() - The Lowercase Examiner

To ascertain if your string is entirely composed of lowercase characters, str.islower() steps forward to provide the verdict.

lowercase = "python"
is_lowercase = lowercase.islower()
print(is_lowercase)
# Output: True

21. str.isupper() - The Uppercase Inquisitor

Similarly, if an uppercase audit is in order, str.isupper() acts as the diligent inquisitor, assessing the case of every character.

uppercase = "PYTHON"
is_uppercase = uppercase.isupper()
print(is_uppercase)
# Output: True

22. str.isnumeric() - The Numeric Inquirer

When you need to verify that your string is entirely composed of numeric characters, str.isnumeric() serves as the numeric inquirer.

numeric = "42"
is_numeric = numeric.isnumeric()
print(is_numeric)
# Output: True

23. str.isdecimal() - The Decimal Inspector

In cases of decimal character evaluation, str.isdecimal() comes into play. It checks if all characters are decimal digits.

decimal = "123.45"
is_decimal = decimal.isdecimal()
print(is_decimal)
# Output: False

24. str.splitlines() - The Line Break Liberator

When youā€™re faced with a string containing multiple lines, str.splitlines() rises to the occasion. It splits the string at line breaks, liberating each line into a list.

poem = "Roses are red\nViolets are blue\nSugar is sweet\nAnd so are you."
lines = poem.splitlines()
print(lines)
# Output:
# ['Roses are red', 'Violets are blue', 'Sugar is sweet', 'And so are you.']

25. str.zfill(width) - The Zero Padding Maestro

In the world of string formatting, str.zfill() is the master of zero padding. It adds zeros to the left of your string until it reaches the specified width.

number = "42"
padded_number = number.zfill(5)
print(padded_number)
# Output: "00042"

And thus, weā€™ve completed our voyage through the realm of Python string methods for the second part. Armed with these tools, you are now well-equipped to navigate the seas of string manipulation with confidence and finesse.

I hope youā€™ve enjoyed this exploration, and may your Python adventures continue with ever-increasing knowledge and expertise! To read on to the last chapter of this, hereā€™s a link: https://medium.com/@ernestasena/python-string-methods-unveiled-part-three-633978031169

Happy Coding!!

--

--