04. Strings methods

Strings have various methods that are used for logical testing. Briefly, let's go over the most commonly used methods, with two strings s and t.

Logical testing

s.isalpha()
Check if string consists of all letters.
s.capitalize()
Return s with first letter and lowercase rest.
s.isalnum()
Check if string s consists of all alphanumeric characters.
s.isdigit()
True if not empty and all characters are digits.
s.numeric()
True if not empty and all numeric.
s.isdecimal()
True if not empty and its characters can be used in forming decimal-radix numbers including unicode number values and names.
s.swapcase()
Return a copy of string with upper converted to lowercase and vice versa.
s.islower()
True if contains at least one cased character and all of its cased characters are lowercase.
s.isupper()
True if all cased characters are uppercase and there is at least one cased character.
s.istitle()
Not empty and all its uppercase characters follow uncased characters and all its lowercase characters follow cased characters.
s.count(i)
Return total occurrences of character i.
s.zfill(w)
Return numeric string filled with zeros for a length of w.

Searching

It's easy to search for a substring. Assume that i is the start position and j is the end position.

s.startswith(t[, i, [j]])
True if s starts with t.
s.endswith(t[, i, [j]])
True if s ends with t.
s.find(t[, i[, j]])
Return the lowest index of s at which t is found. Return -1 if not found.
s.rfind(t[, i[, j]])
Return highest index where t is found.
s.index(t[, i[, j]])
Same as find() but ValueError instead of -1 if not found.
s.rindex(t[, i[, j]])
Same as rfind() but ValueError instead of -1 if not found.
s.count(t[, i[, j]])
returns the number of occurrences of t in s.

Replacing

Searching and replacing is also possible in Python through a handful of functions.

s.replace(t, u)
Replace all occurrences of t in s with u.
s.translate(dict)
Translate string s with a dictionary.

Changing case

s.lower()
Return lowercase form of s.
s.capitalize()
Return capitalized form of s.
s.title()
Return string with each word beginning with an uppercase character and the rest lowercase.
s.swapcase()
Return string with lowercase letters made uppercase and vice versa.

Parsing

Here are some functions that can help you get rid of any leading or trailing characters.

s.lstrip(c)
Return s with leading characters c removed.
s.rstrip(c)
Same as above but with trailing characters removed.
s.strip(c)
Returns s with leading and trailing character c removed.

Learn to be a Pythonista!

Introducing Python

Learn to be a Pythonista! Try Python

Easy to understand and fun to read, Introducing Python is ideal for beginning programmers as well as those new to the language. Author Bill Lubanovic takes you from the basics to more involved and varied topics, mixing tutorials with cookbook-style code recipes to explain concepts in Python 3. End-of-chapter exercises help you practice what you learned.

$ Check price
39.9939.99Amazon 4.5 logo(37+ reviews)

More Python resources

Aching back from coding all day?

Foam Seat Cushion

Aching back from coding all day? Try Back Problems

This foam seat cushion relieves lowerback pain, numbness and pressure sores by promoting healthy weight distribution, posture and spine alignment. Furthermore, it reduces pressure on the tailbone and hip bones while sitting. Perfect for sitting on the computer desk for long periods of time.

$ Check price
99.9599.95Amazon 4.5 logo(9,445+ reviews)

More Back Problems resources

Ad